Fluent Mapping and Version

2 posts, 0 answers
  1. Roderik
    Roderik avatar
    4 posts
    Member since:
    Feb 2008

    Posted 19 Oct 2011 Link to this post

    Dear Telerik, 

    I am trying to use automatic versioning via telerik fluent mapping but I can't seem to get it to work. 

    My basic example: 

    public class Dog
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public long Version { get; set; }
    }

    which has the following mapping: 

    MappingConfiguration<Dog> dogConfiguration = new MappingConfiguration<Dog>();
    dogConfiguration.MapType().ToTable("dogs");
    dogConfiguration.HasProperty(p => p.ID).IsIdentity(KeyGenerator.Autoinc);
    dogConfiguration.HasProperty(p => p.Version).IsVersion().IsNotNullable();
    preparedConfigurations.Add(dogConfiguration);

    and the following unittest: 

    Punt4FluentContext dbContext = new Punt4FluentContext(
       dbConnection, sqlConfiguration);
     
    Dog dog = new Dog();
    dog.Name = "Droopy";
    dbContext.Add(dog);
    dbContext.SaveChanges();
     
    Punt4FluentContext dbContext2 = new Punt4FluentContext(
       dbConnection, sqlConfiguration);
     
    dbContext2.GetAll<Dog>().First().Name = "Barkinson";
    dbContext2.SaveChanges();

    Gives the following result in the database: 

    i_d: 1
    nme: Barkinson (so, it did save it twice)
    vrsion: 0 <-- I expected 2 here!

    What I wish to achieve is make the version field auto increment every time the entity is saved (which in this case is two times). I assumed the method IsVersion() could be used for this. Is this possible or should I implement this functionality in the domain layer?

    Kind regards,
    Roderik Steenbergen

  2. Serge
    Admin
    Serge avatar
    375 posts

    Posted 20 Oct 2011 Link to this post

    Hello Roderik,

     You also need to specify that the class uses version as its concurrency mechanism. You can do so with code that look like: 

    dogConfiguration.WithConcurencyControl(OptimisticConcurrencyControlStrategy.Version)
             .ToTable("dogs");

    However I think that this can be automatically inferred and we will be planning on improving this behaviour. 

    I hope this is helpful. 

    Best wishes,
    Serge
    the Telerik team

    NEW and UPDATED OpenAccess ORM Resources. Check them out!

  3. DevCraft banner
Back to Top