如何管理数据库中的外键插入?

发布于 2024-11-13 19:15:23 字数 1892 浏览 4 评论 0原文

我开发了一个小对象解析器(对于 SQL 文件,它允许我轻松找到过程中使用的所有表的列表)并且我有以下模型:

3 个表:

  • 对象(ID,名称,LastWriteTime)
  • 表(ID,名称) )
  • ObjectTable(ObjectID,TableID)

我有3个相应的类:

Object - 扫描的文件列表:

[Table(Name = "object")]
public class SourceFile : IEquatable<SourceFile>
{
    public SourceFile()
    { }

    [Column(Name = "id", IsDbGenerated = true)]
    public Guid ID
    {
        get;
        set;
    }

    [Column(Name = "lastwritetime")]
    public DateTime LastWriteTimeUtc
    {
        get;
        set;
    }

    [Column(Name = "name", IsPrimaryKey = true)]
    public String Name
    {
        get;
        set;
    }

    public bool Equals(SourceFile other)
    {
        return Name == other.Name;
    }

    public override int GetHashCode()
    {
        return Name.GetHashCode();
    }
}

Table - 数据库中所有表的列表:

[Table(Name = "table")]
public class SourceTable : IEquatable<SourceTable>
{
    public SourceTable()
    { }

    [Column(Name = "id", IsDbGenerated = true)]
    public Guid ID
    {
        get;
        set;
    }

    [Column(Name = "name", IsPrimaryKey = true)]
    public String Name
    {
        get;
        set;
    }

    public bool Equals(SourceTable other)
    {
        return Name == other.Name;
    }

    public override int GetHashCode()
    {
        return Name.GetHashCode();
    }
}

ObjectTable - 对于每个对象,使用的表列表:

[Table(Name = "object_table")]
public class SourceFileTable
{
    [Column(Name = "object_id", IsPrimaryKey = true)]
    public Guid ObjectID
    {
        get; set;
    }

    [Column(Name = "table_id", IsPrimaryKey = true)]
    public Guid TableID
    {
        get; set;
    }
}

如您所见,对象ID &表是 DBGenerate,所以我的问题是,如何使用生成的 ID 在 ObjectTable 中添加值?仅在插入数据库后给出,而不是之前。

有什么提示吗?

I develop a little object parser (for SQL files that allows me to find easily the list of all tables used in a procedure) and I've the following model :

3 tables :

  • Object (ID, Name, LastWriteTime)
  • Table (ID, Name)
  • ObjectTable (ObjectID, TableID)

I've the 3 corresponding classes :

Object - List of files scanned:

[Table(Name = "object")]
public class SourceFile : IEquatable<SourceFile>
{
    public SourceFile()
    { }

    [Column(Name = "id", IsDbGenerated = true)]
    public Guid ID
    {
        get;
        set;
    }

    [Column(Name = "lastwritetime")]
    public DateTime LastWriteTimeUtc
    {
        get;
        set;
    }

    [Column(Name = "name", IsPrimaryKey = true)]
    public String Name
    {
        get;
        set;
    }

    public bool Equals(SourceFile other)
    {
        return Name == other.Name;
    }

    public override int GetHashCode()
    {
        return Name.GetHashCode();
    }
}

Table - List of all tables in the DB:

[Table(Name = "table")]
public class SourceTable : IEquatable<SourceTable>
{
    public SourceTable()
    { }

    [Column(Name = "id", IsDbGenerated = true)]
    public Guid ID
    {
        get;
        set;
    }

    [Column(Name = "name", IsPrimaryKey = true)]
    public String Name
    {
        get;
        set;
    }

    public bool Equals(SourceTable other)
    {
        return Name == other.Name;
    }

    public override int GetHashCode()
    {
        return Name.GetHashCode();
    }
}

ObjectTable - For each object, the list of table used :

[Table(Name = "object_table")]
public class SourceFileTable
{
    [Column(Name = "object_id", IsPrimaryKey = true)]
    public Guid ObjectID
    {
        get; set;
    }

    [Column(Name = "table_id", IsPrimaryKey = true)]
    public Guid TableID
    {
        get; set;
    }
}

As you can see, the ID of object & table are DBGenerated, so my question is, how can I add values in ObjectTable using the generated IDs? There only given once inserted in DB, not before.

Any hint?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏尔 2024-11-20 19:15:23

如果您添加已创建的实体对象,Linq-to-sql 将为您计算出来。只要你的外键在数据库中没问题,这样的事情就会起作用:

using (db = new datacontext())
{ 
    var parent = new parent();
    var child = new child();
    parent.children.ad(child);
    db.parents.insertonsubmit(parent);
    db.submitchanges();
 }

所以你不添加键,而是添加对象。对于像您这样的协会也同样适用。

更新
在你的情况下,它看起来像(用心,所以请检查拼写错误)

using (db = new datacontext())
{
var o = new Object();
var t = new Table();
db.objects.insertonsubmit(o);
db.tables.insertonsubmit(t);
var objecttable = new objecttable();
objecttable.object = o;
objeecttable.tbale = t;
db.objecttables.insertonsubmit(objecttable);
db.submitchanges();

}

或者你的问题是如何在数据库中定义外键?这是通过这样的语句完成的:

ALTER TABLE ORDERS 
ADD FOREIGN KEY (customer_sid) REFERENCES CUSTOMER(SID); 

If you add the entity objects that you have created, Linq-to-sql will figure it out for you. As long as your foreign keys are ok in the DB something like this will work:

using (db = new datacontext())
{ 
    var parent = new parent();
    var child = new child();
    parent.children.ad(child);
    db.parents.insertonsubmit(parent);
    db.submitchanges();
 }

So you do not add the keys but the objects. The same will work for an association like yours.

Update
In your case it will look like (by heart, so please check for typo's)

using (db = new datacontext())
{
var o = new Object();
var t = new Table();
db.objects.insertonsubmit(o);
db.tables.insertonsubmit(t);
var objecttable = new objecttable();
objecttable.object = o;
objeecttable.tbale = t;
db.objecttables.insertonsubmit(objecttable);
db.submitchanges();

}

Or is your question about how to define the foreign keys in the database? That is done with a statement like this:

ALTER TABLE ORDERS 
ADD FOREIGN KEY (customer_sid) REFERENCES CUSTOMER(SID); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文