[英]Zero to One Relationship in EF Code First
I have 2 entities like below; 我有以下两个实体;
public class Order
{
public int Id { get; set; }
public Description { get; set; }
public virtual Purchase Purchase{ get; set;}
}
public class Purchase
{
public int Id { get; set; }
public Description { get; set;}
public int? OrderId { get; set;}
public virtual Order Order { get; set;}
}
Here I create order first. 在这里,我首先创建订单。
How can i specify this relationship in Code First 如何在Code First中指定此关系
You can specify a unique attribute like this. 您可以像这样指定唯一属性。 You just can't make a unique index the target of a foreign key in EF6.
您只是不能将唯一索引作为EF6中外键的目标。
public class Order
{
public int Id { get; set; }
public string Description { get; set; }
public virtual Purchase Purchase { get; set; }
}
public class Purchase
{
public int Id { get; set; }
public string Description { get; set;}
[Index(IsUnique = true)]
public int? OrderId { get; set; }
public virtual Order Order { get; set; }
}
But EF won't allow a 1-1 relationship to a non-key column, but something like this has the desired relational model: 但是EF不允许与非键列建立1-1关系,但是像这样的东西具有所需的关系模型:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp5
{
public class Order
{
public int Id { get; set; }
public string Description { get; set; }
internal ICollection<Purchase> Purchases { get; } = new HashSet<Purchase>();
public Purchase Purchase { get { return Purchases.FirstOrDefault(); } }
}
public class Purchase
{
public int Id { get; set; }
public string Description { get; set; }
[Index(IsUnique = true)]
public int? OrderId { get; set; }
[ForeignKey("OrderId")]
public virtual Order Order { get; set; }
}
public class Db : DbContext
{
public DbSet<Order> Orders { get; set; }
public DbSet<Purchase> Purchases { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Order>().HasMany(o => o.Purchases).WithOptional(p => p.Order);
}
}
class Program
{
static void Main(string[] args)
{
Database.SetInitializer(new DropCreateDatabaseAlways<Db>());
int OrderId;
using (var db = new Db())
{
var o = db.Orders.Create();
o.Description = "New Order";
var p = db.Purchases.Create();
p.Order = o;
p.Description = "New Purchase";
db.Orders.Add(o);
db.Purchases.Add(p);
db.SaveChanges();
OrderId = o.Id;
}
using (var db = new Db())
{
var p = db.Purchases.Create();
p.OrderId = OrderId;
p.Description = "Another Purchase";
db.Purchases.Add(p);
db.SaveChanges(); //fails
}
}
}
}
David 大卫
In the comments to your question, you indicated: 在对问题的评论中,您指出:
I need to make it (OrderId) unique also
我还需要使其(OrderId)唯一
You cannot do that that because EF does not support unique columns except keys. 您不能那样做,因为EF除键外不支持唯一列。
You may use an index which will allow you to make the index unique but you cannot have a unique key . 您可以使用索引,使索引唯一,但不能拥有唯一键 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.