【问题标题】:Entity Framework complex type inserting实体框架复杂类型插入
【发布时间】:2015-08-26 21:30:01
【问题描述】:

我的代码有一个烦人的问题。 我的模型:

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; }
}

现在我通过 dbSet.Find(id) (RepositoryFactory) 从 Db 获取 Option 对象,我想做的是保存新创建的用户,但选择了选项。

如果我这样做:

var option = dbSet.Find(id);
            var user = new User()
            {
                Name = "Name",
                Conference = new Conference
                {
                    Option = option
                }
            };
//...
context.SaveChanges();

我遇到了一个异常: 一个实体对象不能被多个 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);

【问题讨论】:

    标签: c# entity-framework orm insert entity


    【解决方案1】:

    您是否要在用户和会议之间实现 1:1 的关系?如果是这样,您需要向 User 添加一个 Id(键)属性。请参阅我在下面的代码示例中添加的有关 1:1 关系的 cmets。我建议您进一步评估您的领域层,以确保这是您想要实现的目标......

    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();
                }
            }
        }
    }
    

    【讨论】:

    • 这仍然没有帮助。错误仍然存​​在。
    • 我测试了上面的示例代码,它会执行。尝试将上面的所有代码复制/粘贴到控制台应用程序中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多