简体   繁体   中英

Entity Framework Code first wrong saving

I'm having a problem understanding how EF is saving my entities. I have my DbContext :

    public DbSet<Hero> MyHeroes { get; set; }
    public DbSet<SpecialItem> SpecialItems { get; set; }
    public DbSet<Weapon> MyWeapons { get; set; }
    public DbSet<WeaponHolder> MyWeaponHolders { get; set; }

My "Main" entity is Hero , it inherit from Character :

public class Character
    {
        [Key]
        public int CharacterID { get; set; }
        [Column("name")]
        protected string name;
        [Column("maxLife")]
        protected int maxHitPoint;
        [Column("actualLife")]
        protected int actualHitPoint;
        [Column("baseAgility")]
        protected int baseAgility;
    }
public class Hero : Character
    {
        [Column("Gold")]
        private int gold;
        public WeaponHolder weaponHolder;
        public List<SpecialItem> specialItems;
    }

It contains a WeaponHolder :

public class WeaponHolder
    {
        [Key]
        public int WeaponHolderID { get; set; }
        [Column("weaponHolderSize")]
        private int weaponHolderSize;
        List<Weapon> weapons;
    }

which can contain several Weapon :

 public class Weapon : Loot
    {
        [Column("name")]
        string name;
        [Column("WeaponType")]
        WeaponTypes weaponType;
    }

I'm creating a Hero and give him several SpecialItems , and a weaponHolder with several Weapon . When I'm trying to save my Hero , using

heroContext.MyHeroes.Add(hero);

it does save it's SpecialItems but it doesn't save its WeaponHolder and content. If I do save my weaponHolder "manually" using

heroContext.MyWeaponHolders.Add(hero.weaponHolder);

then it does as expected save the WeaponHolder and its Weapons .

I noticed that EF did not create a foreign key between Hero and WeaponHolder , but I did add one manually and did not notice any change in the saving comportement.

Why isn't EF automatically saving my Hero 's WeaponHolder when I am saving my Hero ? Can I save it manually? If so, how can I retrieve my Hero primary Key as soon as I save him in order to manually link my WeaponHolder foreign key to the right value?

With a single entity in Hero (like the WeaponHolder property), it would also have the reference to the ID in it like

public int WeaponHolderID { get; set; }

That is missing from this implementation. Is the relationship you are referring to setup correctly?

Thanks to the help of Brian Mains I managed to get it to work.
I was missing a lot of getter/setter in order to be able to retrieve properly my properties.
And I indeed had to add a new ID field per Key, and put my [ForeignKey("...")] on it.

My code now looks like that :

public class Character
    {
        [Key]
        public int CharacterID { get; set; }
        [Column("name")]
        public string name { get; set; }
        [Column("maxLife")]
        protected int maxHitPoint{ get; set; }
        [Column("actualLife")]
        protected int actualHitPoint{ get; set; }
        [Column("baseAgility")]
        protected int baseAgility{ get; set; }
    }

public class Hero : Character
    {
        [ForeignKey("weaponHolder")]
        public int WeaponHolderID { get; set; }
        [ForeignKey("backPack")]
        public int BackPackID { get; set; }
        [Column("Gold")]
        private int gold{ get; set; }
        public WeaponHolder weaponHolder{ get; set; }
        public List<SpecialItem> specialItems{ get; set; }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM