C# 具有可为空外键的一对一关系
我想在EF中创建一对一关系,其中外键可以为null(因此,它可以称为0..1-to-0..1)C# 具有可为空外键的一对一关系,c#,entity-framework,one-to-one,C#,Entity Framework,One To One,我想在EF中创建一对一关系,其中外键可以为null(因此,它可以称为0..1-to-0..1) FluentAPI配置: modelBuilder.Entity<Sale>() .HasOptional(x => x.ProductInstance) .WithOptionalPrincipal(x => x.Sale); modelBuilder.Entity<Sale>() .HasOptional(x => x.ProductI
FluentAPI
配置:
modelBuilder.Entity<Sale>()
.HasOptional(x => x.ProductInstance)
.WithOptionalPrincipal(x => x.Sale);
modelBuilder.Entity<Sale>()
.HasOptional(x => x.ProductInstance)
.WithOptionalPrincipal(x => x.Sale)
.Map(a => a.MapKey("SaleId"));
如何才能只获取一个列SaleId
,该列将是外键?一对零或一关系:
当一个表的主键在关系数据库(如SQL Server)的另一个表中变为PK&FK时,就会发生一对零或一关系
一对零或数据注释如下:
public class ProductInstance
{
public int Id { get; set; }
public virtual Sale Sale { get; set; }
}
public class Sale
{
[Key]
[ForeignKey("ProductInstance")]
public int ProductInstanceId { get; set; }
///write here other properties
public virtual ProductInstance ProductInstance { get; set; }
}
modelBuilder.Entity<ProductInstance>()
.HasOptional(s => s.Sale)
.WithRequired(ad => ad.ProductInstance);
public class Department
{
[Key]
public int DepartmentId { get; set; }
ForeignKey("Person")]
public int PersonId { get; set; }
public virtual Person Person { get; set; }
}
public class Person
{
[Key]
public int PersonId { get; set; }
[ForeignKey("Department")]
public int? DepartmentId { get; set; }
public virtual Department Department { get; set; }
}
modelBuilder.Entity<Person>()
.HasOptional(pi => pi.Department)
.WithMany()
.HasForeignKey(s => s.DepartmentId);
一对零或使用Fluent API的一,如下所示:
public class ProductInstance
{
public int Id { get; set; }
public virtual Sale Sale { get; set; }
}
public class Sale
{
[Key]
[ForeignKey("ProductInstance")]
public int ProductInstanceId { get; set; }
///write here other properties
public virtual ProductInstance ProductInstance { get; set; }
}
modelBuilder.Entity<ProductInstance>()
.HasOptional(s => s.Sale)
.WithRequired(ad => ad.ProductInstance);
public class Department
{
[Key]
public int DepartmentId { get; set; }
ForeignKey("Person")]
public int PersonId { get; set; }
public virtual Person Person { get; set; }
}
public class Person
{
[Key]
public int PersonId { get; set; }
[ForeignKey("Department")]
public int? DepartmentId { get; set; }
public virtual Department Department { get; set; }
}
modelBuilder.Entity<Person>()
.HasOptional(pi => pi.Department)
.WithMany()
.HasForeignKey(s => s.DepartmentId);
一对一,使用Fluent API,如下所示:
public class ProductInstance
{
public int Id { get; set; }
public virtual Sale Sale { get; set; }
}
public class Sale
{
[Key]
[ForeignKey("ProductInstance")]
public int ProductInstanceId { get; set; }
///write here other properties
public virtual ProductInstance ProductInstance { get; set; }
}
modelBuilder.Entity<ProductInstance>()
.HasOptional(s => s.Sale)
.WithRequired(ad => ad.ProductInstance);
public class Department
{
[Key]
public int DepartmentId { get; set; }
ForeignKey("Person")]
public int PersonId { get; set; }
public virtual Person Person { get; set; }
}
public class Person
{
[Key]
public int PersonId { get; set; }
[ForeignKey("Department")]
public int? DepartmentId { get; set; }
public virtual Department Department { get; set; }
}
modelBuilder.Entity<Person>()
.HasOptional(pi => pi.Department)
.WithMany()
.HasForeignKey(s => s.DepartmentId);
modelBuilder.Entity()
.has可选(pi=>pi.Department)
.有很多
.HasForeignKey(s=>s.DepartmentId);
一对零或一关系:
当一个表的主键在关系数据库(如SQL Server)的另一个表中变为PK&FK时,就会发生一对零或一关系
一对零或数据注释如下:
public class ProductInstance
{
public int Id { get; set; }
public virtual Sale Sale { get; set; }
}
public class Sale
{
[Key]
[ForeignKey("ProductInstance")]
public int ProductInstanceId { get; set; }
///write here other properties
public virtual ProductInstance ProductInstance { get; set; }
}
modelBuilder.Entity<ProductInstance>()
.HasOptional(s => s.Sale)
.WithRequired(ad => ad.ProductInstance);
public class Department
{
[Key]
public int DepartmentId { get; set; }
ForeignKey("Person")]
public int PersonId { get; set; }
public virtual Person Person { get; set; }
}
public class Person
{
[Key]
public int PersonId { get; set; }
[ForeignKey("Department")]
public int? DepartmentId { get; set; }
public virtual Department Department { get; set; }
}
modelBuilder.Entity<Person>()
.HasOptional(pi => pi.Department)
.WithMany()
.HasForeignKey(s => s.DepartmentId);
一对零或使用Fluent API的一,如下所示:
public class ProductInstance
{
public int Id { get; set; }
public virtual Sale Sale { get; set; }
}
public class Sale
{
[Key]
[ForeignKey("ProductInstance")]
public int ProductInstanceId { get; set; }
///write here other properties
public virtual ProductInstance ProductInstance { get; set; }
}
modelBuilder.Entity<ProductInstance>()
.HasOptional(s => s.Sale)
.WithRequired(ad => ad.ProductInstance);
public class Department
{
[Key]
public int DepartmentId { get; set; }
ForeignKey("Person")]
public int PersonId { get; set; }
public virtual Person Person { get; set; }
}
public class Person
{
[Key]
public int PersonId { get; set; }
[ForeignKey("Department")]
public int? DepartmentId { get; set; }
public virtual Department Department { get; set; }
}
modelBuilder.Entity<Person>()
.HasOptional(pi => pi.Department)
.WithMany()
.HasForeignKey(s => s.DepartmentId);
一对一,使用Fluent API,如下所示:
public class ProductInstance
{
public int Id { get; set; }
public virtual Sale Sale { get; set; }
}
public class Sale
{
[Key]
[ForeignKey("ProductInstance")]
public int ProductInstanceId { get; set; }
///write here other properties
public virtual ProductInstance ProductInstance { get; set; }
}
modelBuilder.Entity<ProductInstance>()
.HasOptional(s => s.Sale)
.WithRequired(ad => ad.ProductInstance);
public class Department
{
[Key]
public int DepartmentId { get; set; }
ForeignKey("Person")]
public int PersonId { get; set; }
public virtual Person Person { get; set; }
}
public class Person
{
[Key]
public int PersonId { get; set; }
[ForeignKey("Department")]
public int? DepartmentId { get; set; }
public virtual Department Department { get; set; }
}
modelBuilder.Entity<Person>()
.HasOptional(pi => pi.Department)
.WithMany()
.HasForeignKey(s => s.DepartmentId);
modelBuilder.Entity()
.has可选(pi=>pi.Department)
.有很多
.HasForeignKey(s=>s.DepartmentId);
EF不支持与显式FK属性的一对一
关联-没有HasForeignKey
fluent API,如果您尝试使用ForeignKey
数据注释来解决它,您将在迁移过程中遇到多重性异常
唯一的解决方案是删除ProductInstance.SaleId
属性,结果是model:
public class ProductInstance
{
public int Id { get; set; }
public Sale Sale { get; set; }
}
public class Sale
{
public int Id { get; set; }
public ProductInstance ProductInstance { get; set; }
}
和配置:
modelBuilder.Entity<Sale>()
.HasOptional(x => x.ProductInstance)
.WithOptionalPrincipal(x => x.Sale);
modelBuilder.Entity<Sale>()
.HasOptional(x => x.ProductInstance)
.WithOptionalPrincipal(x => x.Sale)
.Map(a => a.MapKey("SaleId"));
modelBuilder.Entity()
.has可选(x=>x.ProductInstance)
.具有期权本金(x=>x.销售)
.Map(a=>a.MapKey(“SaleId”);
EF不支持与显式FK属性的一对一
关联-没有HasForeignKey
fluent API,如果您尝试使用ForeignKey
数据注释来解决它,您将在迁移过程中遇到多重性异常
唯一的解决方案是删除ProductInstance.SaleId
属性,结果是model:
public class ProductInstance
{
public int Id { get; set; }
public Sale Sale { get; set; }
}
public class Sale
{
public int Id { get; set; }
public ProductInstance ProductInstance { get; set; }
}
和配置:
modelBuilder.Entity<Sale>()
.HasOptional(x => x.ProductInstance)
.WithOptionalPrincipal(x => x.Sale);
modelBuilder.Entity<Sale>()
.HasOptional(x => x.ProductInstance)
.WithOptionalPrincipal(x => x.Sale)
.Map(a => a.MapKey("SaleId"));
modelBuilder.Entity()
.has可选(x=>x.ProductInstance)
.具有期权本金(x=>x.销售)
.Map(a=>a.MapKey(“SaleId”);
为什么要创建与销售实例的关联和保存销售实例Id的属性?这就是为什么要在表中创建2列的原因。EF代表实体框架,所以它只作为实体而不是值对象处理数据。也许在后一个版本中,它还支持value-objects。为什么要创建与Sale实例的关联和保存Sale实例Id的属性?这就是为什么要在表中创建2列的原因。EF代表实体框架,所以它只作为实体而不是值对象处理数据。也许在后一个版本中,它也支持值对象。@Alexey Markov,测试我的代码!!如果它如您所期望的那样工作,请将其标记为帮助其他有类似问题的人的答案!!您的代码可以工作,但在本例中,销售需要ProductInstance,但我不知道这一点。这里的主要问题是可空外键。伊万·斯托夫说,这是不受支持的。因此,我从模型中删除了外键属性,并仅使用导航Property@Alexey马尔科夫,你错过了重点!!在一对一或一对零关系的情况下,您不需要显式地使用可为空的外键!!外键默认为空。根据您的代码,sale默认为空。运行代码并测试它!!我理解你。我的问题是,我希望在模型中使用ForeignKey属性,以便在不加载intance和使用导航属性的情况下轻松建立关系instance.SaleId=…
。但是我明白了,不可能在一对一中获得显式的可为null的外键。所以,现在我只使用导航属性(有点不舒服),现在我不关心外键字段名-SaleId,Sale_id-没关系。很高兴帮助你@Alexey Markov,测试我的代码!!如果它如您所期望的那样工作,请将其标记为帮助其他有类似问题的人的答案!!您的代码可以工作,但在本例中,销售需要ProductInstance,但我不知道这一点。这里的主要问题是可空外键。伊万·斯托夫说,这是不受支持的。因此,我从模型中删除了外键属性,并仅使用导航Property@Alexey马尔科夫,你错过了重点!!在一对一或一对零关系的情况下,您不需要显式地使用可为空的外键!!外键默认为空。根据您的代码,sale默认为空。运行代码并测试它!!我理解你。我的问题是,我希望在模型中使用ForeignKey属性,以便在不加载intance和使用导航属性的情况下轻松建立关系instance.SaleId=…
。但是我明白了,不可能在一对一中获得显式的可为null的外键。所以,现在我只使用导航属性(有点不舒服),现在我不关心外键字段名-SaleId,Sale_id-没关系。很高兴帮助你!!