繁体   English   中英

实体框架复杂类型插入

[英]Entity Framework complex type inserting

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

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对象,我想要做的是保存新创建的User,但是选择了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);

您是否要实现用户与会议之间的1:1关系? 如果是这样,则需要向用户添加一个Id(键)属性。 请参阅我在下面的代码示例中添加的有关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();
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM