[英]Using EntityFramework C# code-first from database - how to map table with no primary key
I am building an Ntier application with EntityFramework c#. 我正在使用EntityFramework c#构建Ntier应用程序。 I am adding an Entity Data Model in my Data Access Layer with code-first approach from existing database.
我正在使用现有数据库中的代码优先方法在数据访问层中添加实体数据模型。
Some of the tables of my db weren't included because they don't have primary key. 我的数据库中的某些表未包括在内,因为它们没有主键。 I have seen some ways to work around this problem, modifying EntityFramework's edmx to force the mapping to the database, disguising some field like a key.
我已经看到了解决此问题的一些方法,可以修改EntityFramework的edmx以强制映射到数据库,伪装某些字段(如键)。 But I am not using the .edmx, since I can't use automatic migrations with it.
但是我没有使用.edmx,因为我不能对其使用自动迁移。 I only generate POCOs from my existing database and then go on with code first migrations.
我仅从现有数据库中生成POCO,然后继续进行代码优先迁移。
Is there a way to force Entity Framework to generate a POCO for those tables without primary key ? 有没有一种方法可以强制Entity Framework为那些没有主键的表生成POCO? Some only have one entry and really don't need PrimaryKey
有些只有一个条目,实际上并不需要PrimaryKey
In the end, I just wrote my own POCOs for the tables that weren't included. 最后,我只是为未包含的表编写了自己的POCO。
I used an attribute [KEY] above the property i wanted to act like key. 我在要用作键的属性上方使用了[KEY]属性。 I added DbSet lines in the DataModel and EF did recognize them in my database.
我在DataModel中添加了DbSet行,EF确实在我的数据库中识别了它们。
I didn't want to generate primary keys because my boss didn't want, and thats a reason good enough. 我不想生成主键,因为老板不想要,这就是一个足够好的理由。 :) Hope the best for you thx for answer
:)希望最适合您的答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.