locked
A value shared across entities or associations is generated in more than one location. RRS feed

  • Question

  • can anyone tell what's wrong with this code.

    public class Product

        {

            public int Id { get; set; }

            public string Name { get; set; }

            public string SpecialName { get; set; }

        }

     

        public class MyContext : DbContext

        {

            public MyContext()

                : base("Data Source=.;Initial Catalog=TestDb;Integrated Security=True"){}

          

            public DbSet<Product> Products { get; set; }

     

            protected override void OnModelCreating(ModelBuilder modelBuilder)

            {

              

                modelBuilder.Entity<Product>()

                        .Map(mc =>

                        {

                            mc.Properties(p => new{p.Id,p.Name});

                            mc.ToTable("Products");

                        })

                        .Map(mc =>

                        {

                            mc.Properties(p => new{p.Id,p.SpecialName});

                            mc.ToTable("SpecialProductInfo");

                        });

            }

        }

     

     var db = new MyContext();

                db.Products.Add(new Product { Name = "Tea", SpecialName = "Special Tea" });

                db.SaveChanges();

     

    I get the following exception.

    A value shared across entities or associations is generated in more than one location.

    I have a presentation in the evening but i cant figure out how to get past this error.


    Zeeshan Hirani Entity Framework 4.0 Recipes by Apress
    http://weblogs.asp.net/zeeshanhirani
    Tuesday, January 25, 2011 4:55 AM

Answers

  • Hi Zeeshan,

    Nothing wrong with your code... this is a bug that we've fixed in our code base since CTP5 was released. Entity splitting should only result in identity being used on one of the tables but CTP5 configures it for all tables. There unfortunately isn't a workaround for CTP5 other than not using identity with table splitting.

    ~Rowan

    Tuesday, January 25, 2011 9:00 PM
    Moderator