CTP4加入表为什么不起作用? [英] CTP4 Join Table why doesn't work ?

查看:67
本文介绍了CTP4加入表为什么不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Northwind数据库,我想建立Employee和Territory的映射,我的鳕鱼在下面。

With Northwind database, I want establish the mapping for Employee and Territory, my cods is below.

模型

班级员工

     {

    {

         public int ID {get;组; }

        public int ID { get; set; }

         public string LastName {get;组; }

        public string LastName { get; set; }

   &NBSP; &NBSP;   public virtual ICollection< Territories>领土{得到;组; }

        public virtual ICollection<Territories> Territories { get; set; }

 

   &NBSP; &NBSP;   public Employee()

        public Employee ( )

   &NBSP; &NBSP;   {

        {

   &NBSP; &NBSP; &NBSP; &NBSP;   Territories = new List< Territories> ();

            Territories = new List<Territories> ( );

   &NBSP; &NBSP;  }

        }

    }

    }

    类别地区

    class Territories

     {

    {

   &NBSP; &NBSP;   public int ID {get;组; }

        public int ID { get; set; }

   &NBSP; &NBSP;   public string Description {get;组; }

        public string Description { get; set; }

   &NBSP; &NBSP;   public virtual ICollection< Employee>员工{get;组; }

        public virtual ICollection<Employee> Employees { get; set; }

    }

    }

 

配置

类EmployeeConfiguration:EntityConfiguration< Employee>

class EmployeeConfiguration : EntityConfiguration<Employee>

   &NBSP; {&NBSP;&NBSP; &NBSP; &NBSP;  

    {        

   &NBSP; &NBSP;   public EmployeeConfiguration()

        public EmployeeConfiguration ( )

   &NBSP; &NBSP;   {

        {

   &NBSP; &NBSP; &NBSP; &NBSP;   HasKey(x => x.ID);

            HasKey ( x => x.ID );

 

   &NBSP; &NBSP; &NBSP; &NBSP;   HasMany(x => x.Territories)

            HasMany ( x => x.Territories )

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   .WithMany(x => x.Employees)

                .WithMany ( x => x.Employees )

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; .MAP(QUOT; EmployeeTerritories" ;,(E,T)=>新建

                .Map ( "EmployeeTerritories", ( e, t ) => new

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {

                {

            EmployeeID = e.ID,

                    EmployeeID = e.ID,

  ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;的TerritoryID = t.ID

                    TerritoryID = t.ID

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;  });

                } );

 

   &NBSP; &NBSP; &NBSP; &NBSP;   MapSingleType(x => new

            MapSingleType ( x => new

        {

            {

   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;雇员= x.ID,

                EmployeeID = x.ID,

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;名字= x.LastName

                LastName = x.LastName

       })

            } )

   &NBSP; &NBSP; &NBSP; &NBSP;   .ToTable(" Employees");

            .ToTable ( "Employees" );

   &NBSP; &NBSP;  }

        }

    }

    }

    类TerritoriesConfiguration:EntityConfiguration< Territories>

    class TerritoriesConfiguration : EntityConfiguration<Territories>

     {

    {

   &NBSP; &NBSP;   public TerritoriesConfiguration()

        public TerritoriesConfiguration ( )

   &NBSP; &NBSP;   {

        {

   &NBSP; &NBSP; &NBSP; &NBSP;   HasKey(x => x.ID);

            HasKey ( x => x.ID );

 

   &NBSP; &NBSP; &NBSP; &NBSP;   MapSingleType(x => new

            MapSingleType ( x => new

        {

            {

           TerritoryID = x.ID,

                TerritoryID = x.ID,

          TerritoryDe​​scription = x.Description,

                TerritoryDescription = x.Description,

       })

            } )

   &NBSP; &NBSP; &NBSP; &NBSP;   .ToTable(" Territories");

            .ToTable ( "Territories" );

   &NBSP; &NBSP;  }

        }

    }

    }

 

背景

class MyContext:ObjectContext

class MyContext : ObjectContext

     {

    {

   &NBSP; &NBSP;   ///< summary>

        /// <summary>

   &NBSP; &NBSP;   /// MyContext的构造函数。

        /// Constructor of MyContext.

   &NBSP; &NBSP;   ///< / summary>

        /// </summary>

   &NBSP; &NBSP;   public MyContext(EntityConnection连接)

        public MyContext ( EntityConnection connection )

   &NBSP; &NBSP; &NBSP; &NBSP;  :基础(连接)

            : base ( connection )

   &NBSP; &NBSP;   {

        {

 

   &NBSP; &NBSP;  }

        }

   &NBSP; &NBSP; &NBSP;对象集<员工> mEmployees;

        ObjectSet<Employee> mEmployees;

   &NBSP; &NBSP;   public ObjectSet< Employee>员工

        public ObjectSet<Employee> Employees

   &NBSP; &NBSP;   {

        {

   &NBSP; &NBSP; &NBSP; &NBSP;  获取

            get

   &NBSP; &NBSP; &NBSP; &NBSP;   {

            {

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   if(mEmployees == null)

                if ( mEmployees == null )

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   mEmployees = CreateObjectSet< Employee> ();

                    mEmployees = CreateObjectSet<Employee> ( );

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  返回mEmployees;

                return mEmployees;

   &NBSP; &NBSP; &NBSP; &NBSP;  }

            }

   &NBSP; &NBSP;  }

        }

   &NBSP; &NBSP; &NBSP;对象集<领土> mTerritories;

        ObjectSet<Territories> mTerritories;

   &NBSP; &NBSP;   public ObjectSet< Territories>地区

        public ObjectSet<Territories> Territories

   &NBSP; &NBSP;   {

        {

   &NBSP; &NBSP; &NBSP; &NBSP;  获取

            get

   &NBSP; &NBSP; &NBSP; &NBSP;   {

            {

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   if(mTerritories == null)

                if ( mTerritories == null )

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   mTerritories = CreateObjectSet< Territories> ();

                    mTerritories = CreateObjectSet<Territories> ( );

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  返回mTerritories;

                return mTerritories;

   &NBSP; &NBSP; &NBSP; &NBSP;  }

            }

   &NBSP; &NBSP;  }

        }

 

测试代码

 

var builder = new ModelBuilder();

var builder = new ModelBuilder ( );

&NBSP;&NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   builder.Configurations.Add(new EmployeeConfiguration());

            builder.Configurations.Add ( new EmployeeConfiguration ( ) );

   &NBSP; &NBSP; &NBSP; &NBSP;   builder.Configurations.Add(new TerritoriesConfiguration());

            builder.Configurations.Add ( new TerritoriesConfiguration ( ) );

   &NBSP; &NBSP; &NBSP; &NBSP;   var model = builder.CreateModel();

            var model = builder.CreateModel ( );

 

   &NBSP; &NBSP; &NBSP; &NBSP;   var connection = new SqlConnection(ConfigurationManager.ConnectionStrings [" MyContext"] .ConnectionString);

            var connection = new SqlConnection ( ConfigurationManager.ConnectionStrings [ "MyContext" ].ConnectionString );

   &NBSP; &NBSP; &NBSP; &NBSP;   using(MyContext context = model.CreateObjectContext< MyContext>(connection))

            using ( MyContext context = model.CreateObjectContext<MyContext> ( connection ) )

   &NBSP; &NBSP; &NBSP; &NBSP;   {

            {

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   foreach(var context in context.Employees)

                foreach ( var employee in context.Employees )

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   {

                {

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   Debug.WriteLine(employee.LastName);

                    Debug.WriteLine ( employee.LastName );

 

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   foreach(employee.Territories中的var项目)//此处,employee.Territories 的计数为0,为什么?

                    foreach ( var item in employee.Territories ) // Here the count of  employee.Territories  is 0 , why?

&NBSP;&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   {

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   Debug.Write("   ");

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   Debug.WriteLine(item.ID);

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  }

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  }

                }

   &NBSP; &NBSP; &NBSP; &NBSP;  }

            }

 

使用这些代码,我可以正确检索员工,但我无法检索到employee.Territories,任何人都可以告诉我为什么以及如何解决它?非常感谢。

With these codes, I can retrieve employee correctly, but I can't retrieve employee.Territories, anybody can tell me why and how to solve it? Thanks a lot.

 

推荐答案

我有一个类似的问题,它没有加载我的父引用对象。

I have a similar problem where it does not load my parent reference object.

不确定问题在哪里。 ..?

Not sure where is the issue. ..?

 


这篇关于CTP4加入表为什么不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆