本文介绍了EF4,仅代码,TPT映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


大家好!


我正在尝试实施和测试TPT映射:


公共类BaseData 
{
public Guid ID {get;组; }
公共字符串Column1 {get;组; }
}


public class InheritedData:BaseData
{
public string Column2 {get;组; }
}


[TestClass]
公共类UnitTest
{
私有静态ContextBuilder< ObjectContext> _builder;

public TestContext TestContext {get;组; }

[ClassInitialize]
public static void Initialize(TestContext testContext)
{
_builder = new ContextBuilder< ObjectContext>();

_builder
.Entity< BaseData>()
.MapHierarchy(data => new {bid = data.ID,data.Column1})
.ToTable (QUOT; 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("数据源=(本地);初始目录= ef.test; Integrated Security = true;");
var context = _builder.Create(conn);

context.AddObject(" Table_2",new InheritedData());
}
}

测试运行结果



UnitTest.AddInheritedData:失败



测试  ;方法&NBSP; TestProject.UnitTest.AddInheritedData&NBSP;投掷&NBSP;异常:&NBSP;

System.InvalidOperationException:&NBSP;多&NBSP;对象&NBSP;设置&NBSP;每&NBSP;类型&NBSP;是&NBSP;不&NBSP;支持&NBSP;所述&NBSP;对象群组。 ;套&NBSP; 'TABLE_1' &NBSP;和&NBSP; 'TABLE_2' &NBSP;可以&NBSP;二者&NBSP;含有&NBSP;实例&NBSP;的&NBSP;类型&NBSP; 'TestProject.BaseData'

解决方案


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'.

解决方案


这篇关于EF4,仅代码,TPT映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 09:41