Storing System.Version in a Database with Entity Framework Code First

I have a database created using Entity Framework 6.1 Code First. I have one specific table that I am having problems with.

I have a table called Manifests, entity definition is below

public class Manifest
    public int Id { get; set; }
    public DateTime ReleaseDate { get; set; }
    public int VersionMajor { get; set; }
    public int VersionMinor { get; set; }
    public int VersionBuild { get; set; }
    public int VersionRevision { get; set; }
    public Version Version
            return new Version(this.VersionMajor, this.VersionMinor, this.VersionBuild, this.VersionRevision);

            this.VersionMajor = value.Major;
            this.VersionMinor = value.Minor;
            this.VersionBuild = value.Build;
            this.VersionRevision = value.Revision;
    public string ReleaseNotes { get; set; }
    public bool IsCompulsory { get; set; }
    public string Description { get; set; }
    public string UpdatePackage { get; set; }


You will notice that I have defined a property that is System.Version

and will keep the manifest version, however when running add-migration in the Package Manager Console, the resulting migration does not include the column Version

. I tried to install modelBuilder.ComplexType<System.Version>()

in OnModelCreating

but to no avail, please help?

As I said, I changed the entity definition (see above), but now I am getting an error when starting the seed, see below:

System.Data.Entity.Core.EntityCommandCompilationException: An error occurred while preparing the command definition. See the inner exception for details. ---> System.Data.Entity.Core.MappingException: 
(6,10) : error 3004: Problem in mapping fragments starting at line 6:No mapping specified for properties Manifest.Version in Set Manifests.
An Entity with Key (PK) will not round-trip when:
  Entity is type [Drogo.Meera.Data.Context.Manifest]


The seed method has the following:

    p => p.Version,
    new Manifest
            Description = "Initial Release",
            VersionMajor = 1,
            VersionMinor = 0,
            VersionBuild = 0,
            IsCompulsory = true,
            ReleaseDate = DateTime.Now,
            ReleaseNotes = "Initial Release",
            UpdatePackage = @"v1-0-0\"



source to share

1 answer

I guess you don't see what you expect when you use it System.Version

as a complex type as it doesn't have any valid properties to use EF.

If a property only has a getter or setter, but not both, it will not be included in the model.

You should be able to work around this by pulling out the properties you want to keep (and mark Manifest.Version

as a property to ignore).

public class Manifest
    // other properties

    int VersionMajor { get; set; }
    int VersionMinor { get; set; }
    int VersionBuild { get; set; }
    int VersionRevision { get; set; }

    public Version Version 
            return new Version(VersionMajor, VersionMinor, VersionBuild, VersionRevision); 
            VersionMajor = value.Major;
            VersionMinor = value.Minor;
            VersionBuild = value.Build;
            VersionRevision = value.Revision;


// Adding [NotMapped] to Manifest.Version would also work, as in the link below
modelBuilder.Entity<Manifest>().Ignore(m => m.Version);


Here's another example: Entity Framework maps multiple columns to a complex C # type



All Articles