2

I want to maintain a version number (e.g. 1.1, 1.3, 2.15, etc.) for my main entity. I can't use decimal to store the version as I have to store 1.1, that will get stored as 1.100 if I use decimal. Hence, I am planning to create a small version controller class which manipulate the version number and returns me major/minor/complete number as required, and want to map that with a varchar column in the database.

Example:

public class Item
{
    public Guid Id {get;set;}

    public string Name {get;set;}

    **// I want to map this property with the varchar column in the database**
    public Version Version {get;set;}
}

public class Version
{
    public string FullVersion {get;set;}

    public string Major {get;set;}

    public string Minor {get;set;}

    public void IncrementMinor()
    { // some logic }

    public void IncrementMajor()
    { // some logic }

    public override string ToString()
    {
        return FullVersion;
    }
}

Any suggestion?

1 Answer 1

4

If you will not define Version as entity (i.e. declare db set on your context), then by default it will be stored as complex type. So, if you will store your Item entity, you will see following fields in database:

 Id
 Name
 Version_FullVersion
 Version_Major
 Version_Minor

All you need to do is marking major and minor properties as not mapped, and give pretty name for full version property:

public class Version
{
    [Column("Version")]
    public string FullVersion 
    {
        get { return String.Format("{0}.{1}", Major, Minor); }
        set
        {
            var parts = value.Split('.');
            Major = parts[0];
            Minor = parts[1];
        }
    }
    [NotMapped]
    public string Major { get; set; }
    [NotMapped]
    public string Minor { get; set; }
    // ...
}

This generates exactly three columns:

 Id
 Name
 Version

Btw consider to use integer type for major and minor version fields.


Another option - make your version property not mapped and add property to store string version value:

public class Item
{
    public Guid Id { get; set; }
    public string Name { get; set; }

    [NotMapped]
    public Version Version { get; set; }

    [Column("Version")]
    public string VersionValue
    {
        get { return Version != null ? Version.ToString() : null; }
        set { Version = Version.Parse(value); }
    }
}

And add Parse method to your Version class (which will create version instance from version string).

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your response, It looks same as the way we can use enum in EF 4.3 Code First. Good idea, I can do that. But it would be great if we can directly map our customized object because many people will be using this entity. I have to restrict them not to use VersionValue property, use Version property always. I can't even make VersionValue property to private.
@Hitesh yes, exactly same approach as with enums. I've updated answer with another approach, which is think would fit your needs
Yes, that looks better solution. Thanks lazyberezovsky (sorry if I didn't call you with your name)
How do I ignore those properties (Major/Minor) through fluent API? I tried modelBuilder.Entity<Item>().Ignore(e => e.Version.Minor), it didn't work.
@Hitesh Version is a complex type. You should configure it this way modelBuilder.ComplexType<Version>().Ignore(v => v.Minor);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.