C# 实体框架代码首先支持这种映射吗?
假设我有这样一个类模型:C# 实体框架代码首先支持这种映射吗?,c#,entity-framework,ef-code-first,C#,Entity Framework,Ef Code First,假设我有这样一个类模型: public class BlogPost { ... [Key] public Guid Id {get;private set;} public virtual ICollection Comments {get;private set;} } public class Comment { [Key] public Guid Id {get;private set;} public st
public class BlogPost
{
...
[Key]
public Guid Id {get;private set;}
public virtual ICollection Comments {get;private set;}
}
public class Comment
{
[Key]
public Guid Id {get;private set;}
public string Text{get;set;}
}
这里不要过多地阅读我的伪代码,但我想知道的是:
comment类是否必须具有Guid BlogPostId或BlogPost父属性
我可以像上面那样对comment类建模,并且仍然可以通过blogpost.Comments属性将其映射到blogpost。
e、 g.通过提供一些其他映射属性
我不希望聚合成员了解任何有关其AR的信息。是的,当post具有注释的导航属性时,您不必在注释实体中指定post id(即FK)或引用:
public class BlogPost
{
public Guid Id { get; private set; }
public virtual ICollection<Comment> Comments { get; private set; }
}
public class Comment
{
public Guid Id { get; private set; }
public string Text { get; set; }
}
哦,很好,我不认为仅仅按照惯例来做这件事是明智的。谢谢:)是否可以为外键命名列,而不为其添加属性?e、 g.如果出于遗留原因希望外键映射到现有列?
modelBuilder.Entity<BlogPost>()
.HasMany(bp => bp.Comments)
.WithRequired()
.Map(c => c.MapKey("PostId"))
.WillCascadeOnDelete();