Entity Framework - Table Per Type Inheritance - Existing Database

纵饮孤独 提交于 2019-12-24 13:52:40

问题


I want to implement Table Per Type inheritance with Entity framework for an existing database.

The database:

The inheritance for ImageParagraphs works perfect, but I am not able to make a Table Per Type inheritance with LinkListParagraph because of the different primary keys (ParagraphID; ParagraphID+LinkID):

Error 1 Error 3003: Problem in Mapping Fragment starting at line 113: All the key properties (Paragraphs.ParagraphID) of the EntitySet Paragraphs must be mapped to all the key properties (LinkListParagraph.LinkID, LinkListParagraph.ParagraphID) of table LinkListParagraph. C:\Users\buc\Documents\Visual Studio 2008\Projects\ParagraphTest\ParagraphTest\ParagraphModel.edmx 114 15 ParagraphTest

Is there a possiblity to solve this problem without changes to the database?

What I want to do is something like this:


回答1:


One way might be to lie to the Entity Framework about the primary key. This would require going into the store mapping in the EDMX and changing the primary key flags. Realize, however, that if you do this then the Update Model from Database wizard will try to "fix" your mapping every time you do an update.

Another way would be to create a view in your database and map the view instead of the table.




回答2:


The main problem comes from the unnecessary default key creations by the entity framework. Try opening .edmx file in xml format and you will now view the following like thing:

<EntityType Name="GSKItemDetails">
          <Key>
            <PropertyRef Name="ItemId" />
            <!--<PropertyRef Name="Description" />
            <PropertyRef Name="NDCNumber" />-->
          </Key>
          <Property Name="ItemId" Type="varchar" Nullable="false" MaxLength="47" />
          <Property Name="Description" Type="varchar" Nullable="false" MaxLength="30" />
          <Property Name="NDCNumber" Type="varchar" Nullable="false" MaxLength="16" />
          <Property Name="UnitPrice" Type="decimal" Precision="19" Scale="4" /> 
        </EntityType>

In my case commentingout the unnecessasary PropertyRefs, as mentioned above, solved the problem of giving error : Error 3003.



来源:https://stackoverflow.com/questions/564975/entity-framework-table-per-type-inheritance-existing-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!