具有继承映射的 linq2db 插入
linq2db insert with inheritance mapping
我正在尝试测试一个复杂的 linq2db 映射示例,其中包括继承映射和嵌入式对象。我按照测试项目中的示例进行操作,但在插入操作时出现异常。我没有在测试项目中找到任何插入或更新操作的例子,所以可能我做错了什么。
[Table]
[Column("SomeString", "SomeModel.SomeString")]
[InheritanceMapping(Code = "code1", Type = typeof(Child1))]
[InheritanceMapping(Code = "code2", Type = typeof(Child2))]
public abstract class Parent
{
[PrimaryKey]
public int Id { get; }
public SomeModel SomeModel { get; private set; }
[Column(IsDiscriminator = true)]
public string DType { get; set; }
protected Parent(int id, SomeModel someModel)
{
Id = id;
SomeModel = someModel;
}
}
public class SomeModel
{
public SomeModel(string someString)
{
SomeString = someString;
}
[NotNull]
public string SomeString { get; }
internal SomeModel()
{
}
}
public class Child1 : Parent
{
public Child1(int id, SomeModel someModel, int threshold) : base(id, someModel)
{
Threshold = threshold;
DType = "child1";
}
[Column]
public int Threshold { get; }
}
public class Child2 : Parent
{
public Child2(int id, SomeModel someModel, string code) : base(id, someModel)
{
Code = code;
DType = "child2";
}
[Column]
public string Code { get; private set; }
}
[Test]
[TestCase("Dont cast child in insert")]
[TestCase("Cast child in insert")]
public void TestInheritanceMapping(string testMode)
{
var db = new DbNorthwind();
db.Execute(@"IF OBJECT_ID('dbo.Parent', 'U') IS NOT NULL
drop table Parent");
db.CreateTable<Parent>();
Console.WriteLine(db.GetTable<Child1>().Select(c => c.Threshold).Any());
switch (testMode)
{
case "Dont cast child in insert":
db.Insert(new Child1(1, new SomeModel("SomeString"), 1));
db.Insert(new Child2(1, new SomeModel("SomeString"), "somecode!"));
break;
case "Cast child in insert":
db.Insert<Parent>(new Child1(1, new SomeModel("SomeString"), 1));
db.Insert<Parent>(new Child2(1, new SomeModel("SomeString"), "somecode!"));
break;
}
}
public class DbNorthwind : DataConnection
{
public DbNorthwind() : base("SqlServer", From.ConnectionStrings.Get("storage.sqlserver"))
{
}
}
在 "Dont cast child in insert" 测试用例中我得到
"System.Data.SqlClient.SqlException : Invalid object name 'Child1'."
在 "Cast child in insert":
"System.ArgumentException : "Threshold" is not a member of type"
但是 table "Parent" 是在 "db.CreateTable();" 步骤中正确创建的:
select 操作似乎也有效
感谢您的帮助!
抱歉回复晚了。
在这种情况下,您应该为父类型指定 table 名称:
[Table("Parent")] // Here it is
[Column("SomeString", "SomeModel.SomeString")]
[InheritanceMapping(Code = "code1", Type = typeof(Child1))]
[InheritanceMapping(Code = "code2", Type = typeof(Child2))]
public abstract class Parent
{
//...
}
我正在尝试测试一个复杂的 linq2db 映射示例,其中包括继承映射和嵌入式对象。我按照测试项目中的示例进行操作,但在插入操作时出现异常。我没有在测试项目中找到任何插入或更新操作的例子,所以可能我做错了什么。
[Table]
[Column("SomeString", "SomeModel.SomeString")]
[InheritanceMapping(Code = "code1", Type = typeof(Child1))]
[InheritanceMapping(Code = "code2", Type = typeof(Child2))]
public abstract class Parent
{
[PrimaryKey]
public int Id { get; }
public SomeModel SomeModel { get; private set; }
[Column(IsDiscriminator = true)]
public string DType { get; set; }
protected Parent(int id, SomeModel someModel)
{
Id = id;
SomeModel = someModel;
}
}
public class SomeModel
{
public SomeModel(string someString)
{
SomeString = someString;
}
[NotNull]
public string SomeString { get; }
internal SomeModel()
{
}
}
public class Child1 : Parent
{
public Child1(int id, SomeModel someModel, int threshold) : base(id, someModel)
{
Threshold = threshold;
DType = "child1";
}
[Column]
public int Threshold { get; }
}
public class Child2 : Parent
{
public Child2(int id, SomeModel someModel, string code) : base(id, someModel)
{
Code = code;
DType = "child2";
}
[Column]
public string Code { get; private set; }
}
[Test]
[TestCase("Dont cast child in insert")]
[TestCase("Cast child in insert")]
public void TestInheritanceMapping(string testMode)
{
var db = new DbNorthwind();
db.Execute(@"IF OBJECT_ID('dbo.Parent', 'U') IS NOT NULL
drop table Parent");
db.CreateTable<Parent>();
Console.WriteLine(db.GetTable<Child1>().Select(c => c.Threshold).Any());
switch (testMode)
{
case "Dont cast child in insert":
db.Insert(new Child1(1, new SomeModel("SomeString"), 1));
db.Insert(new Child2(1, new SomeModel("SomeString"), "somecode!"));
break;
case "Cast child in insert":
db.Insert<Parent>(new Child1(1, new SomeModel("SomeString"), 1));
db.Insert<Parent>(new Child2(1, new SomeModel("SomeString"), "somecode!"));
break;
}
}
public class DbNorthwind : DataConnection
{
public DbNorthwind() : base("SqlServer", From.ConnectionStrings.Get("storage.sqlserver"))
{
}
}
在 "Dont cast child in insert" 测试用例中我得到
"System.Data.SqlClient.SqlException : Invalid object name 'Child1'."
在 "Cast child in insert":
"System.ArgumentException : "Threshold" is not a member of type"
但是 table "Parent" 是在 "db.CreateTable();" 步骤中正确创建的:
select 操作似乎也有效
感谢您的帮助!
抱歉回复晚了。
在这种情况下,您应该为父类型指定 table 名称:
[Table("Parent")] // Here it is
[Column("SomeString", "SomeModel.SomeString")]
[InheritanceMapping(Code = "code1", Type = typeof(Child1))]
[InheritanceMapping(Code = "code2", Type = typeof(Child2))]
public abstract class Parent
{
//...
}