每次我开始研究一个新项目时,我都会和自己进行一场心理辩论,而我正在设计我的POCO.我看过很多教程/代码示例似乎都支持外键关联:
public class Order
{
public int ID { get; set; }
public int CustomerID { get; set; } // <-- Customer ID
...
}
Run Code Online (Sandbox Code Playgroud)
与独立协会相反:
public class Order
{
public int ID { get; set; }
public Customer Customer { get; set; } // <-- Customer object
...
}
Run Code Online (Sandbox Code Playgroud)
我以前使用过NHibernate,并且使用了独立的关联,它们不仅感觉更多OO,而且(延迟加载)的优势在于可以让我访问整个Customer对象,而不仅仅是ID.例如,这允许我检索Order实例,然后Order.Customer.FirstName无需显式地进行连接,这非常方便.
所以回顾一下,我的问题是:
我正在尝试使用RC版本的RS 4.1创建一个快速的ASP.NET MVC 3应用程序.我有两个型号:
public class Race
{
public int RaceId { get; set; }
public string RaceName { get; set; }
public string RaceDescription { get; set; }
public DateTime? RaceDate { get; set; }
public decimal? Budget { get; set; }
public Guid? UserId { get; set; }
public int? AddressId { get; set; }
public virtual Address Address { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
和
public class Address
{
public int AddressId { get; set; }
public string …Run Code Online (Sandbox Code Playgroud) entity-relationship entity-framework-4 ef-code-first entity-framework-4.1 asp.net-mvc-3
我基于这个答案实现了双向1:1关系:
我这样定义双向关系:
public class Student
{
public virtual int StudentId { get; set; }
public virtual Anamnesis Anamnesis { get; set; }
. . .
}
public class Anamnesis
{
[Key, ForeignKey("Student")]
public int AnamnesisId { get; set; }
public virtual Student Student { get; set; }
. . .
}
Run Code Online (Sandbox Code Playgroud)
其中,学生是主要实体,Anamnesis是共享PK的实体.
现在我想创建的关系有一个Delete Rule = CASCADE.实际上,正在创建的关系具有Delete Rule = NO ACTION,如下图所示:

如果我在"表格属性"窗口中手动删除此关系并使用"删除规则"="CASCADE"添加其他关系,则代码将按照我的预期允许我删除学生并且它具有相同ID的共享Anamnesis.
所以,这是我的问题:
有没有办法在我的类中使用Data Annotation(不是Fluent API),以便获得与CASCADE删除规则的关系?我更喜欢使用Data Annotation但是如果不可能的话,我会对使用它的一些Fluent API代码感到满意.
注意
我已经尝试了这篇文章中显示的Fluent API代码.它在我有双向属性的情况下不起作用.
cascade sql-server-ce cascading-deletes ef-code-first entity-framework-4.1
我正在使用EntityFramework 5(或.Net Framework 4.0的4.3)
在我的DbContext对象中,我已经设置了正确的DbSet,并且对象包含对彼此的正确引用.这对我来说并不新鲜,事情也很顺利.
现在在这种情况下,我有一些复合键,有时包括表的外键(在这种情况下是对象).为此,我使用HasKey<>()的功能OnModelCreating中的DbContext的方法.当这些属性具有不同的名称时,没有问题,但是当这些属性具有相同的名称时,无法进行迁移.
一个例子:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// ...
modelBuilder.Entity<PatientVisit>().ToTable("PatientVisits");
modelBuilder.Entity<PatientVisit>().HasKey(x =>
new { x.Patient.Code, x.Code });
// ...
base.OnModelCreating(modelBuilder);
}
Run Code Online (Sandbox Code Playgroud)
正如您在提供的代码中看到的那样,对象PatientVisit具有名为Code的属性,但只要与不同的患者重复该属性,就可以重复此属性.实体Patient还有一个名为Code的密钥.
匿名类型不能有两个推断同名的属性(显而易见).典型的解决方案是将匿名类型的属性命名为:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// ...
modelBuilder.Entity<PatientVisit>().ToTable("PatientVisits");
modelBuilder.Entity<PatientVisit>().HasKey(x =>
new { PatientCode = x.Patient.Code, VisitCode = x.Code });
// ...
base.OnModelCreating(modelBuilder);
}
Run Code Online (Sandbox Code Playgroud)
但是这样做,当我尝试添加迁移时,会抛出此错误消息.
The properties expression 'x => new <>f__AnonymousType3`2(PatientCode
= x.Patient.Code, VisitCode = x.Code)' is not valid. The expression
should represent a property: C#: …Run Code Online (Sandbox Code Playgroud) c# anonymous-types ef-code-first ef-migrations entity-framework-4.3
所以我正在尝试使用Code First和Fluent来映射具有一种派生类型的基类,其中表模式是Table-per-Type排列.此外,派生类型与具有复合外键的另一种类型具有多对一关系.(这些表上的键是不可更改的,名称完全匹配.)
以下是我在CSharp中尝试实现的示例:
public class BaseType
{
public int Id;
public int TenantId;
public int Name;
}
public class DerivedType : BaseType
{
public int Active;
public int OtherTypeId;
public OtherType NavigationProperty;
}
Run Code Online (Sandbox Code Playgroud)
以下是配置类中的配置:
public BaseTypeConfiguration()
{
ToTable("BaseTypes", "dbo");
HasKey(f => new { f.Id, f.TenantId});
Property(f => f.Id)
.HasColumnName("BaseTypeId")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}
public DerivedTypeConfiguration()
{
ToTable("DerivedTypes", "dbo");
//OtherType has many DerivedTypes
HasRequired(dt=> dt.OtherTypeNavigation)
.WithMany(ot=> ot.DerivedTypes)
.HasForeignKey(dt=> new { dt.OtherTypeId, dt.TenantId});
}
Run Code Online (Sandbox Code Playgroud)
从我可以告诉我的映射是正确设置的(例如,我遵循了许多具有这种确切情况但具有单个列标识符的教程和示例)
当我尝试查询这些实体时,我得到的异常是:
The foreign key component 'TenantId' is not a …
c# table-per-type composite-primary-key ef-code-first entity-framework-4.3