[英]Entity Framework Code First generates two foreign key columns in database when creating a 1:n relationship
I want to create an 1:n relationship, based on the naming conventions for creating relationships, with the Entity Framework. 我想基于创建关系的命名约定 ,使用Entity Framework创建1:n关系。 I'm using codefirst.
我正在使用codefirst。 Entity Framework ignores the conventions and alwas creates two foreign key columns, instead of only one:
实体框架忽略了约定,并且alwas创建了两个外键列,而不是只创建一个:
The configuration class is configured like following: 配置类配置如下:
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
I'm using the Microsoft.Identity Framework, with custom classes for all models. 我正在使用Microsoft.Identity Framework,其中包含所有模型的自定义类。
My DbContext class is as simple as following. 我的DbContext类就像下面这么简单。
public class StreamWorkerDataContext : IdentityDbContext<StreamWorkerUser, StreamWorkerRole, int, StreamWorkerUserLogin, StreamWorkerUserRole, StreamWorkerUserClaim>
{
public DbSet<Task> Tasks { get; set; }
}
The Task
model: Task
模型:
public class Task : StreamWorkerEntity
{
public StreamWorkerUser StreamWorkerUser { get; set; }
}
The StreamWorkerUser
model: StreamWorkerUser
模型:
public class StreamWorkerUser : IdentityUser<int, StreamWorkerUserLogin, StreamWorkerUserRole, StreamWorkerUserClaim>
{
public virtual List<Task> Tasks { get; set; } // also tested ICollection
}
The Task
class inherits from this base type, here are also two properties of type StreamWorkerUser
defined (But these columns generated correctly in the database.): Task
类继承自此基类型,这里还定义了StreamWorkerUser
类型的两个属性(但这些列在数据库中正确生成。):
public class StreamWorkerEntity
{
[Key]
public int Id { get; set; }
public DateTimeOffset Created { get; set; }
public DateTimeOffset Updated { get; set; }
public StreamWorkerUser Creator { get; set; }
public StreamWorkerUser LastEditor { get; set; }
}
Is it possible that, the properties Creator
and LastEditor
disturbing the database migration? Creator
和LastEditor
属性是否有可能扰乱数据库迁移?
Do i understand the conventions correctly? 我是否正确理解惯例?
Much more information: 更多信息:
So you might think that EF in this situation: 所以在这种情况下你可能会认为EF:
public StreamWorkerUser Creator { get; set; }
public StreamWorkerUser LastEditor { get; set; }
will create creator_id and lasteditor_id, but it doesn't, because EF took class name and made StreamWorkerUser_Id, and StreamWorkerUser_Id1, because there were StreamWorkerUser_Id already used. 将创建creator_id和lasteditor_id,但它没有,因为EF取了类名并制作了StreamWorkerUser_Id和StreamWorkerUser_Id1,因为已经使用了StreamWorkerUser_Id。 By default, if you make relation by class(not by int field), EF generates its column name depending on the class name(not by field name).
默认情况下,如果按类(而不是int字段)创建关系,则EF会根据类名(而不是字段名称)生成其列名。
If you want to specify column names for those FK you can do : 如果要为这些FK指定列名,您可以执行以下操作:
public int CreatorId { get; set; } //this field name will be column name
[ForeignKey("StreamWorkerUser")]
public StreamWorkerUser Creator { get; set; }
public int EditorId { get; set; } //this field name will also be column name
[ForeignKey("StreamWorkerUser")]
public StreamWorkerUser Editor { get; set; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.