Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EntityFramework对象未设置为模型创建中对象的实例_C#_Asp.net Mvc 3_Entity Framework - Fatal编程技术网

C# EntityFramework对象未设置为模型创建中对象的实例

C# EntityFramework对象未设置为模型创建中对象的实例,c#,asp.net-mvc-3,entity-framework,C#,Asp.net Mvc 3,Entity Framework,我一直在努力将一个在model first中创建的相当大的EntityFramework数据库转换为codefirst。我有一个似乎无法解决的问题。我在调用堆栈上获得一个对象引用,该引用没有设置为具有以下过程的对象实例 ModelConfiguration.Configuration.Types.EntityTypeConfiguration.Configure ModelConfiguration.Configuration.ModelConfiguration.ConfigureEntiti

我一直在努力将一个在model first中创建的相当大的EntityFramework数据库转换为codefirst。我有一个似乎无法解决的问题。我在调用堆栈上获得一个对象引用,该引用没有设置为具有以下过程的对象实例

ModelConfiguration.Configuration.Types.EntityTypeConfiguration.Configure
ModelConfiguration.Configuration.ModelConfiguration.ConfigureEntities
为了简化本文,我创建了一个测试项目,将问题归结为最简单的形式

我有三节课

  • 有a的a有一个可选的b和一个可选的c
  • b有a的集合和c的集合
  • c有一个可选的b和一个a的集合

    public class a
    {
        public int Id { get; set; }
        [Required]
        public string name { get; set; }
    
        public virtual b b { get; set; }
        [ForeignKey("b")]
        public int? b_Id { get; set; }
    
        public virtual c c { get; set; }
        [ForeignKey("c")]
        public int? c_Id { get; set; }
    }
    
    public class b
    {
        public int Id { get; set; }
        public string name { get; set; }
    
        public virtual ICollection<a> a_s { get; set; }
        public virtual ICollection<c> c_s { get; set; }
    }
    
    public class c
    {
        public int Id { get; set; }
    
        public virtual b b { get; set; }
        [ForeignKey("b")]
        public int? b_Id { get; set; }
    
        public virtual ICollection<a> a_s { get; set; }
    }
    
    public class MyContext : DbContext
    {
        public DbSet<a> a { get; set; }
        public DbSet<b> b { get; set; }
        public DbSet<c> c { get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<a>()
                .HasOptional(m => m.b)
                .WithMany(m => m.a_s);
    
            modelBuilder.Entity<b>()
                .HasMany(m => m.c_s)
                .WithRequired(m => m.b);
    
            modelBuilder.Entity<c>()
                .HasMany(m => m.a_s)
                .WithOptional(m => m.c);
    
            base.OnModelCreating(modelBuilder);
        }
    
    }
    

尝试将“a”放入本地内存:

var a = from o in db.a.ToList() select o;

尝试将“a”放入本地内存:

var a = from o in db.a.ToList() select o;

fluent配置和数据注释的混合导致了这个问题。EF团队应该已经处理了这个异常,并给出了一条有意义的错误消息

删除数据注释并使用fluent配置,如下所示

public class a
{
    public int Id { get; set; }

    [Required]
    public string name { get; set; }

    public virtual b b { get; set; }

    public int? b_Id { get; set; }

    public virtual c c { get; set; }

    public int? c_Id { get; set; }
}

public class b
{
    public int Id { get; set; }
    public string name { get; set; }

    public virtual ICollection<a> a_s { get; set; }
    public virtual ICollection<c> c_s { get; set; }
}

public class c
{
    public int Id { get; set; }

    public virtual b b { get; set; }

    public int? b_Id { get; set; }

    public virtual ICollection<a> a_s { get; set; }
}

public class NreContext : DbContext
{
    public DbSet<a> a { get; set; }
    public DbSet<b> b { get; set; }
    public DbSet<c> c { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<a>()
            .HasOptional(m => m.b)
            .WithMany(m => m.a_s)
            .HasForeignKey(m => m.b_Id);

        modelBuilder.Entity<a>()
            .HasOptional(m => m.c)
            .WithMany(m => m.a_s)
            .HasForeignKey(m => m.c_Id);

        modelBuilder.Entity<c>()
            .HasOptional(m => m.b)
            .WithMany(m => m.c_s)
            .HasForeignKey(m => m.b_Id);

        base.OnModelCreating(modelBuilder);
    }
}
公共a类
{
公共int Id{get;set;}
[必需]
公共字符串名称{get;set;}
公共虚拟b{get;set;}
公共int?b_Id{get;set;}
公共虚拟c{get;set;}
公共int?c_Id{get;set;}
}
公共b级
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection a_{get;set;}
公共虚拟ICollection c_s{get;set;}
}
公共c类
{
公共int Id{get;set;}
公共虚拟b{get;set;}
公共int?b_Id{get;set;}
公共虚拟ICollection a_{get;set;}
}
公共类上下文:DbContext
{
公共数据库集a{get;set;}
公共数据库集b{get;set;}
公共数据库集c{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.has可选(m=>m.b)
.有很多(m=>m.a_)
.HasForeignKey(m=>m.b_Id);
modelBuilder.Entity()
.has可选(m=>m.c)
.有很多(m=>m.a_)
.HasForeignKey(m=>m.c_Id);
modelBuilder.Entity()
.has可选(m=>m.b)
.有许多(m=>m.c_)
.HasForeignKey(m=>m.b_Id);
基于模型创建(modelBuilder);
}
}

fluent配置和数据注释的混合导致了此问题。EF团队应该已经处理了这个异常,并给出了一条有意义的错误消息

删除数据注释并使用fluent配置,如下所示

public class a
{
    public int Id { get; set; }

    [Required]
    public string name { get; set; }

    public virtual b b { get; set; }

    public int? b_Id { get; set; }

    public virtual c c { get; set; }

    public int? c_Id { get; set; }
}

public class b
{
    public int Id { get; set; }
    public string name { get; set; }

    public virtual ICollection<a> a_s { get; set; }
    public virtual ICollection<c> c_s { get; set; }
}

public class c
{
    public int Id { get; set; }

    public virtual b b { get; set; }

    public int? b_Id { get; set; }

    public virtual ICollection<a> a_s { get; set; }
}

public class NreContext : DbContext
{
    public DbSet<a> a { get; set; }
    public DbSet<b> b { get; set; }
    public DbSet<c> c { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<a>()
            .HasOptional(m => m.b)
            .WithMany(m => m.a_s)
            .HasForeignKey(m => m.b_Id);

        modelBuilder.Entity<a>()
            .HasOptional(m => m.c)
            .WithMany(m => m.a_s)
            .HasForeignKey(m => m.c_Id);

        modelBuilder.Entity<c>()
            .HasOptional(m => m.b)
            .WithMany(m => m.c_s)
            .HasForeignKey(m => m.b_Id);

        base.OnModelCreating(modelBuilder);
    }
}
公共a类
{
公共int Id{get;set;}
[必需]
公共字符串名称{get;set;}
公共虚拟b{get;set;}
公共int?b_Id{get;set;}
公共虚拟c{get;set;}
公共int?c_Id{get;set;}
}
公共b级
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection a_{get;set;}
公共虚拟ICollection c_s{get;set;}
}
公共c类
{
公共int Id{get;set;}
公共虚拟b{get;set;}
公共int?b_Id{get;set;}
公共虚拟ICollection a_{get;set;}
}
公共类上下文:DbContext
{
公共数据库集a{get;set;}
公共数据库集b{get;set;}
公共数据库集c{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity()
.has可选(m=>m.b)
.有很多(m=>m.a_)
.HasForeignKey(m=>m.b_Id);
modelBuilder.Entity()
.has可选(m=>m.c)
.有很多(m=>m.a_)
.HasForeignKey(m=>m.c_Id);
modelBuilder.Entity()
.has可选(m=>m.b)
.有许多(m=>m.c_)
.HasForeignKey(m=>m.b_Id);
基于模型创建(modelBuilder);
}
}

也会出现同样的问题。问题在于创建模型,这也会带来同样的问题。问题在于创建修复问题的模型。谢谢看起来你应该能够混合和匹配注释和流畅的语法,但我想我会切换到流畅的所有东西来解决这个问题。谢谢看起来您应该能够混合和匹配注释和流畅的语法,但我想我会切换到流畅的