CTP5:参考& ReferenceID [英] CTP5: Trouble with Reference & ReferenceID

查看:89
本文介绍了CTP5:参考& ReferenceID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在使用CTP5,我的Northwind型号是:

Hello, I'm using CTP5, my Northwind model is:

 

   public class NorthwindContext:DbContext

    {

        public NorthwindContext()

            :base(" Northwind")

        {

        }
        public DbSet< Category>类别{get;组; }
        public DbSet< Product>产品{get;组; }


        // protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)

        // {

        //&NBSP;&NBSP;&NBSP; modelBuilder.Conventions.Remove< System.Data.Entity.Database.IncludeMetadataConvention>();

        //}



    }




    [表格("类别")]]
   公共类别类别
    {

        [Key]

        public int CategoryId {get;组; }


        [列(名称=" CategoryName")]
$
        [StringLength(3)]
$
        public string Name {get;组; }


        // [NotMapped]

        // public int NotMappedProp {get;组; }


       公共虚拟ICollection<产品>产品{get;组; }
    }


    [表("产品")]]
   公共类产品

    {

        [Key]

        public int ProductId {get;组; }
        [Column(Name =" ProductName")]
$
        public string Name {get;组; }
        public int? CategoryId {get;组; }
        // [Association(" ass1"," CategoryId"," CategoryId")]]
       公共虚拟类别类别{get;组; }
    }

   public class NorthwindContext : DbContext
    {
        public NorthwindContext()
            : base("Northwind")
        {
        }
        public DbSet<Category> Categories { get; set; }
        public DbSet<Product> Products { get; set; }

        //protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
        //{
        //    modelBuilder.Conventions.Remove<System.Data.Entity.Database.IncludeMetadataConvention>();
        //}

    }


    [Table("Categories")]
    public class Category
    {
        [Key]
        public int CategoryId { get; set; }

        [Column(Name = "CategoryName")]
        [StringLength(3)]
        public string Name { get; set; }

        //[NotMapped]
        //public int NotMappedProp { get; set; }

        public virtual ICollection<Product> Products { get; set; }
    }

    [Table("Products")]
    public class Product
    {
        [Key]
        public int ProductId { get; set; }
        [Column(Name = "ProductName")]
        public string Name { get; set; }
        public int? CategoryId { get; set; }
        //[Association("ass1","CategoryId","CategoryId")]
        public virtual Category Category { get; set; }
    }

 

不要
传递
测试

我不明白
< span title ="Нажмите,чтобыувидетьальтернативныйперевод">什么

错了?

Do not pass the test , I do not understand what is wrong?

            var p1 = db.Products.Find(1);

           

            var c1 = p1.Category;

            var c2 = db.Categories.Find(2);

            p1.Category = c2;

            //不等于

           断言(p1.CategoryId,p1.Category.CategoryId);
$


            ; var p7 = db.Products.Find(7);

            p7.CategoryId = 1;

            // p7.Category == null

           断言(p7.CategoryId,p7.Category.CategoryId);

            var p1 = db.Products.Find(1);
           
            var c1 = p1.Category;
            var c2 = db.Categories.Find(2);
            p1.Category = c2;
            // not equal
            Assert(p1.CategoryId, p1.Category.CategoryId);

            var p7 = db.Products.Find(7);
            p7.CategoryId = 1;
            // p7.Category==null
            Assert(p7.CategoryId, p7.Category.CategoryId);

推荐答案

谢尔盖,

 

模型中的实体是简单的CLR类,没有任何特殊行为。 
这意味着当您在其中一个对象上设置属性时,除了正在设置的属性之外没有任何其他事情发生。 
特别是,设置FK属性不会导致导航属性更改为其他实体,反之亦然。 
保持FK属性和导航属性同步的过程通常被称为"fixup"。 
您可以通过以下几种方式在您的实体中进行修正。

The entities in your model are simple CLR classes without any specialized behavior.  This means that when you set a property on one of your objects nothing happens other than that property being set.  In particular, setting an FK property does not cause the navigation property to another entity to be changed, or vice versa.  The process of keeping FK property and navigation property in sync is often called “fixup”.  There are a few ways that you can get fixup happening in your entities.

·         
上下文可以对您跟踪的实体进行修正。  
您可以通过以下方式执行此操作:

·         The context can do fixup on your tracked entities.  You do this by calling:

 

               
context.ChangeTracker.DetectChanges();

                context.ChangeTracker.DetectChanges();

 

               
事实上,当你调用许多其他方法时,上下文会自动调用DetectChanges。

                In fact the context calls DetectChanges for you automatically when you call many of its other methods.

·         
您可以在您的类中添加修正代码,以便当属性发生变化时,FK会自动更新。 
请注意,此代码变得很复杂。

·         You can add fixup code to your classes such that when a property changes, the FK is automatically updated.  Note that this code becomes complicated fast.

·         
你可以赚
所有虚拟实体的属性(并遵循其他规则
此处 )。 
当您查询实体时,您将获得一个更改跟踪代理,该代理会覆盖您的所有属性并为您执行修正。 
但是请注意,这仅适用于作为代理创建的实体,并且只有当它们被上下文跟踪时才会生效...例如,使用
运算符不会成为代理;请参阅DbSet<> .Create()以获取替代方案。 
更改跟踪代理也有一些微妙的问题,可能会使其他事情变得更加困难。

·         You can make all properties of your entities virtual (and follow some other rules here).  When you query for an entity you will then get a change-tracking proxy that overrides all your properties and performs fixup for you.  Note however that this only works for entities that are created as proxies and only when they are being tracked by a context—for example, an entity created with the new operator will not be a proxy; see DbSet<>.Create() for an alternative.  Change-tracking proxies also have some subtle issues that can make other things more difficult.

 

谢谢,

Arthur


这篇关于CTP5:参考&amp; ReferenceID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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