我需要在Entity Framework Code First中绑定一些关系但我不知道如何。我深深地搜索过,我尝试了很多方法,但仍然没有运气。我想我需要使用流畅的API。
组织有许多 Notes 。 项目有许多 Notes 。 Notes 有一个组织或项目。
我们的想法是将每个实体( Organization 或 Project )绑定到 Note 实体中的一列: SourceKey
public class Organization
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ID { get; set; }
[Required(ErrorMessage = "A name is required.")]
public string Name { get; set; }
public virtual ICollection<Project> Projects { get; set; }
public virtual ICollection<Note> Notes { get; set; }
}
public class Project
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ID { get; set; }
[Required(ErrorMessage = "An organization is required.")]
[Display(Name = "Organization")]
public Guid OrganizationID { get; set; }
[Required(ErrorMessage = "A name is required.")]
public string Name { get; set; }
public virtual Organization Organization { get; set; }
public virtual ICollection<Note> Notes { get; set; }
}
public class Note
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ID { get; set; }
public Guid SourceKey { get; set; }
[Required(ErrorMessage = "A title is required.")]
public string Title { get; set; }
[Column(TypeName = "ntext")]
public string Value { get; set; }
public string Tags { get; set; }
}
所以数据的例子可以是:
组织
7846ac27-d490-4483-8f0b-975a11333dea, Google
项目
3446ac27-d490-4323-8121-921a11333dac, Search
7846ac27-8497-5683-213b-933a11233abc, Maps
注释
1236ac27-d490-4323-8121-921a11333dac, 7846ac27-d490-4483-8f0b-975a11333dea, A note for Google organization
2346ac27-d490-4323-8121-921a11335aab, 7846ac27-d490-4483-8f0b-975a11333dea, Another note for Google organization
3456ac27-d490-4323-8121-921a11331bcc, 7846ac27-8497-5683-213b-933a11233abc, Just a note for Maps project
我不需要从Notes(向上)导航到Organization或Project。如果我有一个组织或项目,我将需要导航(下)到Notes。
答案 0 :(得分:1)
如上所述,如果您不需要备注中的导航属性,则可以省略它们。
如果您在Note
类中包含两个字段以用于外键关联:
public class Note
{
// Code
public Guid OrganizationId { get; set; }
public Guid ProjectId { get; set; }
}
我认为大部分笔记都不属于Organisation
和Project
(尽管他们可以)。
您应该能够使用流畅的API配置您的映射,如下所示:
modelBuilder.Entity<Organization>()
.HasMany(o => o.Notes) // Many Notes
.WithOptional() // No navigation property on Notes
.HasForeignKey(n => n.OrganizationId ); // Use OrganizationId as a foreign key
modelBuilder.Entity<Project>()
.HasMany(o => o.Notes)
.WithOptional()
.HasForeignKey(n => n.ProjectId);
答案 1 :(得分:0)
您可以定义不带导航属性的Note实体
public class Note
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ID { get; set; }
public Guid SourceKey { get; set; }
[Required(ErrorMessage = "A title is required.")]
public string Title { get; set; }
[Column(TypeName = "ntext")]
public string Value { get; set; }
public string Tags { get; set; }
public int OrganizationId { get; set; }
public int ProjectId { get; set; }
}
答案 2 :(得分:0)
如果您不需要导航属性,只需使用以下类定义它:
public class Note
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid NoteID { get; set; }
public Guid SourceKey { get; set; }
[Required(ErrorMessage = "A title is required.")]
public string Title { get; set; }
[Column(TypeName = "ntext")]
public string Value { get; set; }
public string Tags { get; set; }
public Guid OrganizationId { get; set; }
public Guid ProjectId { get; set; }
}
注意OrganizationId
和ProjectId
的类型为Guid
。
答案 3 :(得分:0)
我认为不可能对多个外键使用一列(SourceKey)。它会引发错误。我很确定这是可能的,但也许我对MySQL中的MyISAM表感到困惑。我将使用Note模型中的外键列作为Chris建议。感谢大家。