C# 实体框架复杂类型插入
我的代码有一个恼人的问题。 我的模型:C# 实体框架复杂类型插入,c#,entity-framework,orm,insert,entity,C#,Entity Framework,Orm,Insert,Entity,我的代码有一个恼人的问题。 我的模型: public class Option { public int Id { get; set; } public string Name { get; set; } public virtual ICollection<Conference> Conference { set; get; } } public partial class Conference { [Key, ForeignKey("User")
public class Option
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Conference> Conference { set; get; }
}
public partial class Conference
{
[Key, ForeignKey("User")]
public int UserId { get; set; }
public virtual Option Option { set; get; }
public virtual User User { get; set; }
}
public partial class User
{
public int Id {get; set; }
public string Name { get; set; }
public virtual Conference Conference { get; set; }
}
我有个例外:
一个实体对象不能被多个IEntityChangeTracker实例引用
我做错了什么
编辑:我试图创建映射,但这似乎不起作用
modelBuilder.Entity<Conference>()
.HasKey(x => x.UserId)
.HasRequired(x => x.User)
.WithOptional(user => user.Conference);
modelBuilder.Entity<Option>()
.HasMany(option => option.Conferences)
.WithRequired(conference => conference.Option)
.HasForeignKey(conference => conference.UserId);
modelBuilder.Entity()
.HasKey(x=>x.UserId)
.HasRequired(x=>x.User)
.WithOptional(用户=>user.Conference);
modelBuilder.Entity()
.HasMany(option=>option.Conferences)
.WithRequired(conference=>conference.Option)
.HasForeignKey(conference=>conference.UserId);
您是否试图在用户和会议之间实现1:1的关系?如果是这样,您需要向用户添加一个Id(key)属性。请参阅我在下面的代码示例中添加的关于1:1关系的注释。我会建议进一步评估你的领域层,以确保这是你试图实现的
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
namespace Stackoverflow
{
public class EntityContext : DbContext
{
public IDbSet<Conference> Conferences { get; set; }
public IDbSet<Option> Options { get; set; }
public IDbSet<User> Users { get; set; }
}
public class Option
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Conference> Conference { set; get; }
}
public class Conference
{
// In one-to-one relation one end must be principal and second end must be dependent.
// User is the one which will be inserted first and which can exist without the dependent one.
// Conference end is the one which must be inserted after the principal because it has foreign key to the principal.
[Key, ForeignKey("User")]
public int UserId { get; set; }
public int OptionId { get; set; }
public virtual Option Option { set; get; }
public virtual User User { get; set; }
}
public class User
{
// user requires a key
public int Id { get; set; }
public string Name { get; set; }
public virtual Conference Conference { get; set; }
}
class Program
{
static void Main(string[] args)
{
using (var entityContext = new EntityContext())
{
// added to facilitate your example
var newOption = new Option {Name = "SomeOptionName"};
entityContext.Options.Add(newOption);
entityContext.SaveChanges();
var option = entityContext.Options.Find(newOption.Id);
var user = new User
{
Name = "Name",
Conference = new Conference
{
Option = option
}
};
// need to add the user
entityContext.Users.Add(user);
//...
entityContext.SaveChanges();
}
}
}
}
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.ComponentModel.DataAnnotations.Schema;
使用System.Data.Entity;
命名空间堆栈溢出
{
公共类EntityContext:DbContext
{
公共IDbSet会议{get;set;}
公共IDbSet选项{get;set;}
公共IDbSet用户{get;set;}
}
公共类选项
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection会议{set;get;}
}
公开课会议
{
//在一对一关系中,一端必须是主体,另一端必须是从属的。
//用户是将首先插入的用户,并且可以在没有依赖用户的情况下存在。
//会议结束必须插入主体之后,因为它具有主体的外键。
[密钥,外国密钥(“用户”)]
public int UserId{get;set;}
public int OptionId{get;set;}
公共虚拟选项选项{set;get;}
公共虚拟用户用户{get;set;}
}
公共类用户
{
//用户需要一个密钥
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟会议{get;set;}
}
班级计划
{
静态void Main(字符串[]参数)
{
使用(var entityContext=new entityContext())
{
//添加以方便您的示例
var newOption=newOption{Name=“SomeOptionName”};
entityContext.Options.Add(新选项);
entityContext.SaveChanges();
var option=entityContext.Options.Find(newOption.Id);
var user=新用户
{
Name=“Name”,
会议=新会议
{
选项
}
};
//需要添加用户
entityContext.Users.Add(用户);
//...
entityContext.SaveChanges();
}
}
}
}
您是否试图在用户和会议之间实现1:1的关系?如果是这样,您需要向用户添加一个Id(key)属性。请参阅我在下面的代码示例中添加的关于1:1关系的注释。我会建议进一步评估你的领域层,以确保这是你试图实现的
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
namespace Stackoverflow
{
public class EntityContext : DbContext
{
public IDbSet<Conference> Conferences { get; set; }
public IDbSet<Option> Options { get; set; }
public IDbSet<User> Users { get; set; }
}
public class Option
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Conference> Conference { set; get; }
}
public class Conference
{
// In one-to-one relation one end must be principal and second end must be dependent.
// User is the one which will be inserted first and which can exist without the dependent one.
// Conference end is the one which must be inserted after the principal because it has foreign key to the principal.
[Key, ForeignKey("User")]
public int UserId { get; set; }
public int OptionId { get; set; }
public virtual Option Option { set; get; }
public virtual User User { get; set; }
}
public class User
{
// user requires a key
public int Id { get; set; }
public string Name { get; set; }
public virtual Conference Conference { get; set; }
}
class Program
{
static void Main(string[] args)
{
using (var entityContext = new EntityContext())
{
// added to facilitate your example
var newOption = new Option {Name = "SomeOptionName"};
entityContext.Options.Add(newOption);
entityContext.SaveChanges();
var option = entityContext.Options.Find(newOption.Id);
var user = new User
{
Name = "Name",
Conference = new Conference
{
Option = option
}
};
// need to add the user
entityContext.Users.Add(user);
//...
entityContext.SaveChanges();
}
}
}
}
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.ComponentModel.DataAnnotations.Schema;
使用System.Data.Entity;
命名空间堆栈溢出
{
公共类EntityContext:DbContext
{
公共IDbSet会议{get;set;}
公共IDbSet选项{get;set;}
公共IDbSet用户{get;set;}
}
公共类选项
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection会议{set;get;}
}
公开课会议
{
//在一对一关系中,一端必须是主体,另一端必须是从属的。
//用户是将首先插入的用户,并且可以在没有依赖用户的情况下存在。
//会议结束必须插入主体之后,因为它具有主体的外键。
[密钥,外国密钥(“用户”)]
public int UserId{get;set;}
public int OptionId{get;set;}
公共虚拟选项选项{set;get;}
公共虚拟用户用户{get;set;}
}
公共类用户
{
//用户需要一个密钥
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟会议{get;set;}
}
班级计划
{
静态void Main(字符串[]参数)
{
使用(var entityContext=new entityContext())
{
//添加以方便您的示例
var newOption=newOption{Name=“SomeOptionName”};
entityContext.Options.Add(新选项);
entityContext.SaveChanges();
var option=entityContext.Options.Find(newOption.Id);
var user=新用户
{
Name=“Name”,
会议=新会议
{
选项
}
};
//需要添加用户
entityContext.Users.Add(用户);
//...
entityContext.SaveChanges();
}
}
}
}
这仍然没有帮助。错误仍然存在。我测试了上面的示例代码,它将执行。尝试将上面的所有代码复制/粘贴到控制台应用程序中。这仍然没有帮助。错误仍然存在。我测试了上面的示例代码