1

I am using EntityFramework to save a project version number to database .On the UI page user types in version(major,Minor,Build) integer values and click save button Before I save ,I want to ensure that no duplicate version is getting created in the DB.

what I am trying is to make sure that major.minor.build combination is unique

ProjVersion newVersion=new ProjVersion ();
newVersion.Major=10;
newVersion.Minor=1;
newVersion.Build=1;
this.repository.Add<ProjVersion>(newVersion);
//here how can I ensure that no duplicate versions are added to database
 this.repository.SaveChanges();

[Serializable]
    public class ProjVersion 
    {
        [Key]
        public int Version_Id { get; set; }
        public int Major { get; set; }
        public int Minor { get; set; }
        public int Build { get; set; }
    }

2 Answers 2

2

It sounds like you need to use a Compound Key, where all properties (columns) are part of the Primary Key.

{
    [Key, Column(Order = 0)]
    public int Major { get; set; }

    [Key, Column(Order = 1)]
    public int Minor { get; set; }

    [Key, Column(Order = 2)]
    public int Build { get; set; }
}

Attempts to insert records with matching keys will produce an Exception.

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

4 Comments

While this would achieve the desired result of not allowing duplicate entries, it also brings the complications of a compound primary key with it, such as not allowing any of those fields to be modified.
If this is for version planning I could see the use in changing a Version, but if it's representing built versions (which I assume it is since it includes a Build property) then why would you alter a version? It would be a different version--wouldn't it?
We can't really tell for sure without the full project specification, but people make mistakes. If they accidentally input version 10.1.1 as 10.1.2, there would be no way to change that short of deleting the row. And what if this project does not allow for deletion? Then it has to remain there, and what happens when they actually get to version 10.1.2?
If you don't have full specifications then you need to make justifiable decisions--being defensive is good, but determine how you think things should work and code accordingly. Coding around all possible scenarios doesn't make up for the lack of spec.
1

Check if there are any entries in the database that have the same details as what you're trying to add. If not, then it's a new version and you should add it, if so then it's a duplicate and you should do whatever you want to handle that situation.

if (repository.Get(x => x.Major == newVersion.Major && 
    x.Minor == newVersion.Minor && x.Build == newVersion.Build)
    .Count() > 0)
{
     //notify the user that they are making a duplicate entry
}
else
{
     repository.SaveChanges();
}

5 Comments

And add "using (TransactionScope tsTransScope = new TransactionScope())" around it.
version_id is an auto generated number.so each time a new record is added ,it will generate a new number as version_id .But here what I am trying is to make sure that major.minor.build combination is unique
@Millar, yeah, that was a mistake on my part. It's been edited to show the correct code now.
@Ubikuity why is the transaction scope needed? I've done very similar stuff to this in a couple of projects and never used that.
@BlakeHood the query to check if the version exists and the creation of the desired version should be in an SQL transaction. Otherwise, if 2 users creates a record exactly at the same time, you have a risk that user2 inserts a record after user1 made the SELECT query but before user1 makes the INSERT query.

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.