开发者

How to manage the foreign keys insertions in DB?

开发者 https://www.devze.com 2023-03-10 09:32 出处:网络
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 :

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>
{
   开发者_StackOverflow中文版 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?


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); 
0

精彩评论

暂无评论...
验证码 换一张
取 消