zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    log4j 使用笔记整理中
    执行bat文件
    excel让每个单元格的宽度随着字体自动变动的两种方式(有更好方法的大神,请忽略,求评论下)
    XML中CDATA及其字符实体的使用
    Java文件读写操作指定编码方式。。。。。
    尾数为0零BigDecimal不能装成正常数
    jquery 自动补全控件(支持IE6)待整理
    $.ajax提交,后台接受到的值总是乱码?明天再总结
    js定义变量需赋予初始值
    存储过程的优缺点
  • 原文地址:https://www.cnblogs.com/PerfectBeauty/p/9259503.html
Copyright © 2011-2022 走看看