我收到了以下代码:
public class SqlData : DataContext
{
public Table<TextbausteinTyp> TextbausteinTypen;
public SqlData(string connectionString) : base(connectionString) { }
public void CreateDb()
{
CreateDatabase();
}
}
[Table(Name = "tqTextbausteinTyp")]
public class TextbausteinTyp
{
[Column(IsPrimaryKey = true, DbType = "BigInt IDENTITY NOT NULL", IsDbGenerated = true, UpdateCheck = UpdateCheck.Never)]
public int Id;
[Column]
public string Name;
}
[Table(Name = "tqTextbaustein")]
public class Textbaustein
{
[Column(IsPrimaryKey = true, DbType = "BigInt IDENTITY NOT NULL", IsDbGenerated = true, UpdateCheck = UpdateCheck.Never)]
public int Id;
[Column]
public TextbausteinTyp IdBausteintyp;
[Column]
public string Inhalt;
[Column]
public string Name;
}
如您所见,这很简单。执行CreateDatabase()时收到以下错误:
无法确定&#39; tqOrder.data.TextbausteinTyp&#39;
的SQL类型
连接字符串指向由此代码创建的MSSQL数据库(但没有表格)
这里出了什么问题?
答案 0 :(得分:3)
无法确定'tqOrder.data.TextbausteinTyp'的SQL类型
我并不感到惊讶,因为我无法确定它的SQL类型。表如何成为另一个表中的列?
我想,但不能确定,你真的想要这样的东西:
[Column(CanBeNull=false,DbType="int not null")]
public int IdBausteintyp;
为了有一个引用IdBausteintyp
列的外键列(我更喜欢以“id”结尾,而不是从它开始,但这是另一回事。)
你可以从那个起点然后添加:
private EntityRef<TextbausteinTyp> _textbausteinTyp;
[Association(Storage = "_textbausteinTyp", ThisKey = "IdBausteintyp")]
public TextbausteinTyp TextbausteinTyp
{
get { return _textbausteinTyp.Entity; }
set { _textbausteinTyp.Entity = value; }
}
然后你有一个TextbausteinTyp
属性来获取或设置与外键相关的TextbausteinTyp
。 (注意,如果您只想在查询中明确地加入,则不必执行此操作)。
同样,您可以选择添加TextbausteinTyp
课程:
private EntitySet<Textbaustein> _textbausteins;
[Association(Storage = "_textbausteins", OtherKey = "IdBausteintyp")]
public EntitySet<Textbaustein> Textbausteins
{
get { return _textbausteins; }
set { _textbausteins.Assign(value); }
}
以便映射的另一面;与该特定Textbaustein
相关的TextbausteinTyp
个实体的集合。
答案 1 :(得分:2)
该错误试图告诉您LinqToSQL无法确定哪种SQL Server类型应该存储TextbausteinTyp
类型的对象。
LINQ to SQL不支持用户定义的类型 - 只有原始类型。它将CLR类型与定义的here特定SQL Server类型相匹配。
也就是说,我可以推断您正在尝试定义Textbaustein
和TextbausteinTyp
之间的关联。您可以使用AssociationAttribute执行此操作。以下是有关如何执行此操作的修改代码示例。您可能需要撤消关联。
using System;
using System.Data.Linq;
using System.Data.Linq.Mapping;
namespace LinqConsoleApp
{
public class SqlData : DataContext
{
public Table<TextbausteinTyp> TextbausteinTypen;
public Table<Textbaustein> Textbausteins;
public SqlData(string connectionString)
: base(connectionString)
{
}
}
[Table(Name = "tqTextbausteinTyp")]
public class TextbausteinTyp
{
[Column(IsPrimaryKey = true, DbType = "BigInt IDENTITY NOT NULL", IsDbGenerated = true, UpdateCheck = UpdateCheck.Never)]
public int Id;
[Column]
public string Name;
}
[Table(Name = "tqTextbaustein")]
public class Textbaustein
{
[Column(IsPrimaryKey = true, DbType = "BigInt IDENTITY NOT NULL", IsDbGenerated = true, UpdateCheck = UpdateCheck.Never)]
public int Id;
private EntityRef<TextbausteinTyp> _TextbausteinTyp;
[Association(Storage = "_TextbausteinTyp", ThisKey = "Id")]
public TextbausteinTyp IdBausteintyp;
[Column]
public string Inhalt;
[Column]
public string Name;
}
internal class Program
{
private static void Main(string[] args)
{
var builder = new System.Data.SqlClient.SqlConnectionStringBuilder();
builder["Data Source"] = "<SERVERNAME>"
builder["integrated Security"] = true;
builder["Initial Catalog"] = "<DATABASE>";
SqlData db = new SqlData(builder.ConnectionString);
db.CreateDatabase();
Console.ReadLine();
}
}
}