C# 实体框架:添加到新实体时,如何使用实体B中的autogen ID作为实体A中的外键?
我有两个实体,其中实体A有实体B的外键。实体PK在DB中自动生成。当我为每个实体创建一个新对象并将实体a链接到实体B时,我希望SaveChanges()在保存实体B之后和保存实体a之前更新实体a中的外键,但这不会发生-我是否期望太多?是否必须使用两个SaveChanges()-调用 代码:C# 实体框架:添加到新实体时,如何使用实体B中的autogen ID作为实体A中的外键?,c#,entity-framework,C#,Entity Framework,我有两个实体,其中实体A有实体B的外键。实体PK在DB中自动生成。当我为每个实体创建一个新对象并将实体a链接到实体B时,我希望SaveChanges()在保存实体B之后和保存实体a之前更新实体a中的外键,但这不会发生-我是否期望太多?是否必须使用两个SaveChanges()-调用 代码: 这对我来说很好: using System; using System.Linq; using System.Collections.Generic; using System.ComponentModel.
这对我来说很好:
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Data;
using System.Diagnostics;
using System.Threading.Tasks;
namespace Ef6Test
{
public class Product
{
[Key]
public int ProductId { get; set; }
[Required]
public int ProductTypeId { get; set; }
[ForeignKey("ProductTypeId")]
public virtual ProductType ProductType { get; set; }
}
public class ProductType
{
[Key]
public int ProductTypeId { get; set; }
}
class Db : DbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<ProductType> ProductTypes { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
class Program
{
static void Main(string[] args)
{
Database.SetInitializer(new DropCreateDatabaseAlways<Db>());
using (var db = new Db())
{
Product product = new Product();
product.ProductType = new ProductType();
db.Products.Add(product);
db.SaveChanges(); // works fine
}
Console.WriteLine("Hit any key to exit");
Console.ReadKey();
}
}
}
使用系统;
使用System.Linq;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.ComponentModel.DataAnnotations.Schema;
使用System.Data.Entity;
使用系统数据;
使用系统诊断;
使用System.Threading.Tasks;
命名空间Ef6Test
{
公共类产品
{
[关键]
public int ProductId{get;set;}
[必需]
public int ProductTypeId{get;set;}
[外键(“ProductTypeId”)]
公共虚拟ProductType ProductType{get;set;}
}
公共类ProductType
{
[关键]
public int ProductTypeId{get;set;}
}
类Db:DbContext
{
公共数据库集产品{get;set;}
公共数据库集产品类型{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
基于模型创建(modelBuilder);
}
}
班级计划
{
静态void Main(字符串[]参数)
{
SetInitializer(新的DropCreateDatabaseAlways());
使用(var db=new db())
{
产品=新产品();
product.ProductType=新产品类型();
db.Products.Add(产品);
db.SaveChanges();//工作正常
}
Console.WriteLine(“按任意键退出”);
Console.ReadKey();
}
}
}
这对我来说很好:
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Data;
using System.Diagnostics;
using System.Threading.Tasks;
namespace Ef6Test
{
public class Product
{
[Key]
public int ProductId { get; set; }
[Required]
public int ProductTypeId { get; set; }
[ForeignKey("ProductTypeId")]
public virtual ProductType ProductType { get; set; }
}
public class ProductType
{
[Key]
public int ProductTypeId { get; set; }
}
class Db : DbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<ProductType> ProductTypes { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
class Program
{
static void Main(string[] args)
{
Database.SetInitializer(new DropCreateDatabaseAlways<Db>());
using (var db = new Db())
{
Product product = new Product();
product.ProductType = new ProductType();
db.Products.Add(product);
db.SaveChanges(); // works fine
}
Console.WriteLine("Hit any key to exit");
Console.ReadKey();
}
}
}
使用系统;
使用System.Linq;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.ComponentModel.DataAnnotations.Schema;
使用System.Data.Entity;
使用系统数据;
使用系统诊断;
使用System.Threading.Tasks;
命名空间Ef6Test
{
公共类产品
{
[关键]
public int ProductId{get;set;}
[必需]
public int ProductTypeId{get;set;}
[外键(“ProductTypeId”)]
公共虚拟ProductType ProductType{get;set;}
}
公共类ProductType
{
[关键]
public int ProductTypeId{get;set;}
}
类Db:DbContext
{
公共数据库集产品{get;set;}
公共数据库集产品类型{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
基于模型创建(modelBuilder);
}
}
班级计划
{
静态void Main(字符串[]参数)
{
SetInitializer(新的DropCreateDatabaseAlways());
使用(var db=new db())
{
产品=新产品();
product.ProductType=新产品类型();
db.Products.Add(产品);
db.SaveChanges();//工作正常
}
Console.WriteLine(“按任意键退出”);
Console.ReadKey();
}
}
}
谢谢!我正在使用一个自定义的提供者(用于Interbase),这可能就是为什么它不起作用的原因。我将不得不调试更多;至少我现在知道它应该起作用了。再次感谢!谢谢我正在使用一个自定义的提供者(用于Interbase),这可能就是为什么它不起作用的原因。我将不得不调试更多;至少我现在知道它应该起作用了。再次感谢!