• EF CORE 学习


    关于导航属性

    Figure 9.2 The examples that follow use these entity classes. OneEntity is an optional, one-to-one relationship, because the foreign key back to MyEntity is nullable. OneEntity can exist in the database without being linked to MyEntity (its foreign key will be null). The ManyEntity entity class provides the Add command—creating a new entity/row in the database

    关于状态

    Figure 9.1 The code on the left uses all the standard ways of creating, updating, and deleting data in a database. The middle column, Entity State, shows the EF Core State of the entity as it moves through each of these stages.

    添加导航属性

    Figure 9.3 Adding an entity with both an is tracked and a not tracked relationship. The is tracked case is shown in step 2: a tracked OneEntity instance is set to Modifiedbecause a foreign key in that entity was set. The not tracked case is shown in step 3: a new ManyEntity entity instance is added to the myEntity’s entity instance Manycollection navigational property.

    Table 9.1 Examples of using the Add method, with and without relationships

    EF Core code

    Entity’s state

    IsModified == true

    var entity = new MyEntity();
    entity.MyString = "Test";
    context.Add(entity);

    entity: Added

     

    var entity = new MyEntity();
    var oneToOne = new OneEntity();
    entity.OneToOne = oneToOne;
    context.Add(entity);

    entity: Added

    oneToOne: Added

     

    var entity = new MyEntity();
    var oneToOne =
        context.OneEntities
        .First();
    entity.OneToOne = oneToOne;
    context.Add(entity);

    entity: Added

    oneToOne: Modified

    entity.OneToOne

    oneToOne. MyEntityId

    See Note a

    关于实体跟踪

     
    Figure 9.5 The default way that EF Core finds whether anything has been changed. EF Core holds a tracking snapshot of any entities loaded as tracked entities—any query that doesn’t include the AsNoTracking method. When SaveChanges is called, EF Core, by default, runs the DetectChanges method, which compares tracked entities with the tracking snapshot and sets the State of the entities that have been modified to Modified.

    Table 9.3 Examples of modifying an entity, with and without relationships

    EF Core code

    Entity’s state

    IsModified == true

    var entity =
       context.MyEntities
       .First();
    entity.MyString = “Changed”;

    entity: Modified

    entity.MyString

    var entity =
       context.MyEntities
       .First();
    var oneToOne = new OneEntity();
    entity.OneToOne = oneToOne;

    entity: Unchanged

    OneToOne: Added

     

    var entity =
       context.MyEntities
       .First();
    var oneToOne =
        context.OneEntities
       .First();
    entity.OneToOne = oneToOne;

    entity: Added

    oneToOne: Modified

    entity.OneToOne

    oneToOne.MyEntityId

    关于实体属性修改的事件,但是不建议使用

    Listing 9.1 NotifyEntity entity class, using NotificationEntity class for events
     
     
     
    public class NotifyEntity : NotificationEntity
    {
        private int _id;             //
        private string _myString;    //
        private NotifyOne _oneToOne; //
     
        public int Id
        {
            get => _id;
            set => SetWithNotify(value, ref _id); //
        }
     
        public string MyString
        {
            get => _myString;
            set => SetWithNotify(value, ref _myString); //
        }
     
        public NotifyOne OneToOne
        {
            get => _oneToOne;
            set => SetWithNotify(value, ref _oneToOne); //
        }
     
        public ICollection<NotifyMany>
            Collection { get; } //
            = new ObservableHashSet<NotifyMany>(); //
    }

    public class NotificationEntity : INotifyPropertyChanged
    { public event PropertyChangedEventHandler PropertyChanged; protected void SetWithNotify<T>(T value, ref T field, [CallerMemberName] string propertyName = "")
    // { if (!Object.Equals(field, value)) // { field = value; // PropertyChanged?.Invoke(this, // new PropertyChangedEventArgs(propertyName)); // } } }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    { modelBuilder .Entity<NotifyEntity>() .HasChangeTrackingStrategy( ChangeTrackingStrategy.ChangedNotifications); }

    modelBuilder .HasChangeTrackingStrategy( ChangeTrackingStrategy.ChangedNotifications);

    Figure 9.6 The updating of an entity with both a “Db generated key and not default value” and a “Not Db generated key, or key is default value” relationship. The “Db generated key and not default value” case is shown in step 2: a tracked OneEntity instance is set to Modified because a foreign key in that entity was set. The “Not Db generated key, or key is default value” case is shown in step 3: a new ManyEntity entity instance is added to the myEntity’s entity instance Many collection navigational property.

  • 相关阅读:
    总结在ssm整合中,Mybatis出现Mapped Statements collection already contains value for xxxxx的解决方案
    一般二叉树的创建,前序,中序,后序遍历
    无向图的广度优先遍历和深度优先遍历(简易实现)
    为什么局部内部类中访问同一方法中的变量,该变量一定要是final修饰的
    uml统一建模语言学习笔记(一)
    Font Awesome 字体使用方法, 兼容ie7+
    Java的三种代理模式&完整源码分析
    xxl-job源码分析
    MySQl看这一篇就够了
    第二部分:Spring中配置mongodb
  • 原文地址:https://www.cnblogs.com/PerfectBeauty/p/9259503.html
Copyright © 2020-2023  润新知