如何在C#中插入依赖LINQ到SQL实体而不进行手动ID更新?

如何在C#中插入依赖LINQ到SQL实体而不进行手动ID更新?,c#,database,entity-framework,linq-to-sql,insert,C#,Database,Entity Framework,Linq To Sql,Insert,我有两个模型 型号Foo [System.Data.Linq.Mapping.Table(Name = ...)] public class Foo { [System.Data.Linq.Mapping.Column(IsPrimaryKey = true, IsDbGenerated = true)] public int ID { get; set; } [System.Data.Linq.Mapping.Column(IsPrimaryKey = true)] publ

我有两个模型

型号Foo

[System.Data.Linq.Mapping.Table(Name = ...)]
public class Foo
{
  [System.Data.Linq.Mapping.Column(IsPrimaryKey = true, IsDbGenerated = true)]
  public int ID { get; set; }

  [System.Data.Linq.Mapping.Column(IsPrimaryKey = true)]
  public int BarID { get; set; }
  
  // 1:1 association - foo <> bar
  [System.Data.Linq.Mapping.Association(Name = "FK_Foo_Bar", Storage = "mBar", OtherKey = "ID", ThisKey = "BarID")]
  public Bar CurrentRevision
  {
    get { return mBar.Entity; }
    set { mBar.Entity = value; }
  }
  
  private System.Data.Linq.EntityRef<Bar> mBar = new System.Data.Linq.EntityRef<Bar>();
}
[System.Data.Linq.Mapping.Table(Name = ...)]
public class Foo
{
  [System.Data.Linq.Mapping.Column(IsPrimaryKey = true, IsDbGenerated = true)]
  public int ID { get; set; }

  [System.Data.Linq.Mapping.Column]
  public int BarID { get; set; }

  // 1:1 association - foo <> bar
  [System.Data.Linq.Mapping.Association(Name = "FK_Foo_Bar", Storage = "mBar", OtherKey = "ID",
    ThisKey = "BarID", IsForeignKey=true)]
  public Bar CurrentRevision
  {
    get { return mBar.Entity; }
    set { mBar.Entity = value; }
  }

  private System.Data.Linq.EntityRef<Bar> mBar = new System.Data.Linq.EntityRef<Bar>();
}
lFoo
行和
lBar
行添加到数据库中,但
FooID
BarID
字段保持为零


有没有办法让框架使用正确的ID自动更新列
FooID
BarID
,而不是手动更新?

以下问题导致了这一点,正如我在MSDN上阅读时发现的:

  • 一个类中有多个
    IsPrimaryKey
    表示一个复合键,DB没有
  • 缺少
    IsForeignKey
    属性
  • 在调用
    InsertOnSubmits()
    后直接双向链接对象将无法工作,因为这会引发异常
  • 所以正确的方法是

    型号Foo

    [System.Data.Linq.Mapping.Table(Name = ...)]
    public class Foo
    {
      [System.Data.Linq.Mapping.Column(IsPrimaryKey = true, IsDbGenerated = true)]
      public int ID { get; set; }
    
      [System.Data.Linq.Mapping.Column(IsPrimaryKey = true)]
      public int BarID { get; set; }
      
      // 1:1 association - foo <> bar
      [System.Data.Linq.Mapping.Association(Name = "FK_Foo_Bar", Storage = "mBar", OtherKey = "ID", ThisKey = "BarID")]
      public Bar CurrentRevision
      {
        get { return mBar.Entity; }
        set { mBar.Entity = value; }
      }
      
      private System.Data.Linq.EntityRef<Bar> mBar = new System.Data.Linq.EntityRef<Bar>();
    }
    
    [System.Data.Linq.Mapping.Table(Name = ...)]
    public class Foo
    {
      [System.Data.Linq.Mapping.Column(IsPrimaryKey = true, IsDbGenerated = true)]
      public int ID { get; set; }
    
      [System.Data.Linq.Mapping.Column]
      public int BarID { get; set; }
    
      // 1:1 association - foo <> bar
      [System.Data.Linq.Mapping.Association(Name = "FK_Foo_Bar", Storage = "mBar", OtherKey = "ID",
        ThisKey = "BarID", IsForeignKey=true)]
      public Bar CurrentRevision
      {
        get { return mBar.Entity; }
        set { mBar.Entity = value; }
      }
    
      private System.Data.Linq.EntityRef<Bar> mBar = new System.Data.Linq.EntityRef<Bar>();
    }
    
    不幸的是,作为一个侧节点,在我的例子中,使用
    IsForeignKey
    属性值会改变查询行为(例如,LINQ to SQL生成交叉连接而不是内部连接)和整个程序逻辑

    [System.Data.Linq.Mapping.Table(Name = ...)]
    public class Bar
    {
      [System.Data.Linq.Mapping.Column(IsPrimaryKey = true, IsDbGenerated = true)]
      public int ID { get; set; }
    
      [System.Data.Linq.Mapping.Column]
      public int FooID { get; set; }
    
      // 1:1 association - bar <> foo
      [System.Data.Linq.Mapping.Association(Name = "FK_Bar_Foo", Storage = "mFoo", OtherKey = "ID",
        ThisKey = "FooID", IsForeignKey=true)]
      public Foo Folder
      {
        get { return mFoo.Entity; }
        set { mFoo.Entity = value; }
      }
    
      private System.Data.Linq.EntityRef<Foo> mFoo = new System.Data.Linq.EntityRef<Foo>();
    }
    
    lFoo = new Foo();
    DataContext.Foos.InsertOnSubmit(lFoo);    
    
    lBar = new Bar();
    DataContext.Bars.InsertOnSubmit(lBar);    
    
    DataContext.SubmitChanges();    
    
    lBar = lFoo; // link foo to bar
    lFoo = lBar; // link bar to foo
    
    DataContext.SubmitChanges();