locked
EF4, Code Only, TPT mapping RRS feed

  • Question

  • Hello all!

    I'm trying to implement and test TPT mapping:

    	public class BaseData
    	{
    		public Guid ID { get; set; }
    		public string Column1 { get; set; }
    	}
    
    
    	public class InheritedData : BaseData
    	{
    		public string Column2 { get; set; }
    	}
    
    
    	[TestClass]
    	public class UnitTest
    	{
    		private static ContextBuilder<ObjectContext> _builder;
    
    		public TestContext TestContext { get; set; }
    
    		[ClassInitialize]
    		public static void Initialize(TestContext testContext)
    		{
    			_builder = new ContextBuilder<ObjectContext>();
    
    			_builder
    				.Entity<BaseData>()
    				.MapHierarchy(data => new { bid = data.ID, data.Column1 })
    				.ToTable("Table_1");
    
    			_builder.RegisterSet<BaseData>("Table_1");
    
    
    			_builder
    				.Entity<InheritedData>()
    				.MapHierarchy(data => new { iid = data.ID, data.Column2 })
    				.ToTable("Table_2");
    
    			_builder.RegisterSet<BaseData>("Table_2");
    
    		}
    
    
    		[TestMethod]
    		public void AddInheritedData()
    		{
    			var conn = new SqlConnection("Data Source=(local);Initial Catalog=ef.test;Integrated Security=true;");
    			var context = _builder.Create(conn);
    
    			context.AddObject("Table_2", new InheritedData());
    		}
    	}

    Test run results:

    UnitTest.AddInheritedData : Failed

    Test method TestProject.UnitTest.AddInheritedData threw exception: 
    System.InvalidOperationException: Multiple object sets per type are not supported. The object sets 'Table_1' and 'Table_2' can both contain instances of type 'TestProject.BaseData'.
    at Microsoft.Data.Objects.EntityContainerGenerator`1.EntityTypeDetails.RegisterContainingEntitySet(EntitySetDetails containingEntitySet)
    at Microsoft.Data.Objects.EntityContainerGenerator`1.EntityTypeDetails.RegisterEntitySet(EntityTypeDetailsCollection entityTypesString setName)
    at Microsoft.Data.Objects.EntityContainerGenerator`1.FindEntitySets()
    at Microsoft.Data.CodeFirst.Utility.TraceIndentBlock(String descriptionAction act)
    at Microsoft.Data.Objects.EntityContainerGenerator`1..ctor(ConfigurationSnapshot`1 snapshot)
    at Microsoft.Data.Objects.EntityContainerGenerator`1.GenerateEntityContainer(ConfigurationSnapshot`1 snapshot)
    at Microsoft.Data.Objects.ContextBuilder`1.CreateWorkspace(String providerString providerManifestTokenDbProviderServices providerServices)
    at Microsoft.Data.Objects.ContextBuilder`1.Create(DbConnection storeConnection)
    at TestProject.UnitTest.AddInheritedData() in UnitTest.cs: line 57

     

     

    Question:  is this a bug or did i do somethimg wrong ?

    Wednesday, June 30, 2010 11:30 AM

All replies

  • Hi Dmitry,

    You don't need to call RegisterSet here. The calls to MapHierarchy will automatically create a single Entity Set for the hierarchy.

    Cheers,

    Andrew.

    Monday, July 12, 2010 4:19 PM