mardi 4 août 2015

How not to set DatabaseGeneratedOption.Identity for Seed entities

We had BaseEntity defined as following:

public abstract class BaseEntity
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid Id { get; set; }
        ...
}

As a result of Ids were genereated by DB. However, now we are going to add Seed data. I added the following Seed code:

            Country c = new Country()
            {
                Id = new Guid("251D06A5-E864-4B9F-803B-00E409B0F5AB"),
                Name = "Oceania",
                RegionType = RegionTypeEnum.Region,
                ParentCountryId = worldId
            };
            AddOrUpdate(c);

In this case my "Id" was ignored and new Id was generated. Thus Seeds always created new countries.

Thus I do not need database to generate Id (but I want to use BaseEntity as base class). How do you suggest to deal with this?

Aucun commentaire:

Enregistrer un commentaire