简体   繁体   English

从数据库使用EntityFramework C#代码优先-如何在没有主键的情况下映射表

[英]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.

相关问题 如何使用Entity Framework代码优先映射带有复合键的继承表? - How do I map an inherited table with a composite key using Entity Framework code-first? 使用MVC5.2中的EntityFramework 6.1.1从现有数据库初始化Code​​-First实体 - Initializing Code-First entities from existing database using EntityFramework 6.1.1 in MVC5.2 使用EntityFramework,代码优先(CTP5),如何创建数据库? - Using EntityFramework, code-first (CTP5), how do you create the database? EF代码优先如何使用复合键从表中读取A - EF Code-First How To Read A From Table With A Composite Key 在没有主键的实体中使用表(使用实体代码优先迁移) - Use Table in Entity with no Primary Key (Using Entity Code-first migrations) 首先使用Entity Framework代码创建复杂的主键 - Create a complex Primary Key using Entity Framework code-first 实体框架,代码优先,如何从多对多关系映射中向表添加主键? - Entity Framework, code-first, how to add primary key to table out of many-to-many relationship mapping? 如何使用EntityFramework代码先将tinyint映射到C#字节 - How to map tinyint to c# byte using entityframework code first migration EntityFramework 代码优先迁移忽略 [Key] 并强制使用复合键 - EntityFramework Code-first migration ignoring [Key] and forcing composite key 实体框架代码优先设置的外键和同一表中的主键 - Entity Framework code-first set foreign key with primary key in same table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM