“Update model from database”

2019-08-18 19:56发布

I 'm using Visual Studio and working on an app based on MVC. I'm trying to add some tables to my model. I have used option "Update model from database". The new tables appeared in my edmx file. I have rebuilt my solution but the classes for new tables are missing. Is there any trick to generate the classes every time i wanna add new tables? Or do i have to do it manually? When I set 'Code Generation Strategy' to 'Default' I'm getting hundred of errors similar to this one Error 117 The type 'Mvc4.Models.DPR_MM' already contains a definition for 'DPR_TS'.
I have removed the whole Model. While searching the whole solution for DPR_MM I'm getting these results: Find all "DPR_MM", Subfolders, Find Results 1, Entire Solution, ""

  C:\\Mvc4\Controllers\HomeController.cs(41):                    var DPR_MM = from d in ctx.DPR_MM
  C:\\Mvc4\Controllers\HomeController.cs(45):                    var arr = new object[DPR_MM.Count()];
  C:\\Mvc4\Controllers\HomeController.cs(47):                    foreach (var d in DPR_MM)
  C:\\Mvc4\Models\Model1.edmx(9):          <EntitySet Name="DPR_MM" EntityType="Model.Store.DPR_MM" store:Type="Tables" Schema="QF" />
  C:\\Mvc4\Models\Model1.edmx(45):        <EntityType Name="DPR_MM">
  C:\\Mvc4\Models\Model1.edmx(366):          <EntitySet Name="DPR_MM" EntityType="Model.DPR_MM" />
  C:\\Mvc4\Models\Model1.edmx(389):        <EntityType Name="DPR_MM">
  C:\\Mvc4\Models\Model1.edmx(715):          <EntitySetMapping Name="DPR_MM">
  C:\\Mvc4\Models\Model1.edmx(716):            <EntityTypeMapping TypeName="Model.DPR_MM">
  C:\\Mvc4\Models\Model1.edmx(717):              <MappingFragment StoreEntitySet="DPR_MM">
  C:\\Mvc4\Models\Model1.edmx.diagram(8):        <EntityTypeShape EntityType="Model.DPR_MM" Width="1.5" PointX="0.75" PointY="0.75" IsExpanded="true" />
  Matching lines: 11    Matching files: 3    Total files searched: 168

So I don't see any DPR_MM class. What I am doing wrong?

1条回答
Root(大扎)
2楼-- · 2019-08-18 20:55

Edit

Now that I know you are getting an error, look at the generated code (you could even do a solution search for your class DPR_MM to find all instances of this class). I would guess you have a class Mvc4.Models.DPR_MM in your project that already has DPR_TS as a member and code generation sees that and won't generate another one, kind of like the following:

public partial class DPR_MM : EntityObject
{
    public global::System.Int32 DPR_TS
    {
    ...
    }
}

public partial class DPR_MM : EntityObject
{
    //maybe code generation fails because it doesn't want to do this a second time knowing it would be ambiguous 
    public global::System.Int32 DPR_TS  
    {
    ...
    }
}

You should always be able to generate a new model unless there is some database or namespace problem so make sure you don't have extra files laying around like @Mamu says. Or you could just google it:
first hit
another answer





Initial answer

Do a build. You should then see all your new classes based on the tables you selected when you "updated model from database" (obviously you have to select the new tables when "updating model from database")

查看更多
登录 后发表回答