Reordering LINQ to SQL Attachments

I have two LINQ to SQL classes, CandyBar and DeliciousCandyBar, which map to tables with the same name in SQL Server.

Between CandyBar and DeliciousCandyBar there is a relationship of 0..1. that is, CandyBar can have 0 or 1 DeliciousCandyBars. Conversely, DeliciousCandyBar has exactly one CandyBar.

In the LINQ to SQL class, they look (mostly) similar to

class CandyBar {
  public int Id { get;set;} // this is primary key w/ autoincrement identity
  public string Name {get;set;}
  public EntityRef<DeliciousCandyBar> DeliciousCandyBar {get;set;}
}

class DeliciousCandyBar {
  public int DeliciousnessFactor {get;set;}
  public int CandyBarId {get;set;} // FK to candyBar Id
  public EntityRef<CandyBar> CandyBar {get;set;} // the association property of the FK
}

To feed the database (via l2sql), my crawler goes out and finds sweets and delicious candy.

But with the first tasty candy, my crawler inserts into the CandyStoreDataContext, the DataContext throws an exception when calling SubmitChanges.

. , . , DSL, . , .

var dc = CandyStoreDataContext();
var bar = new CandyBar() {
    Name = "Flake",
    DeliciousCandyBar = new DeliciousCandyBar() {
      DeliciousnessFactory = 12
    }
};

dc.CandyBars.InsertOnSubmit(bar);

dc.SubmitChanges();

SubmitChanges() SqlException " INSERT FOREIGN KEY FK_CandyBar_DeliciousCandyBar. CandyStoreData, " dbo.DeliciousCandyBar ", " CandyBarId ".

, CandyStoreDataContext.Log Console.Out, . LINQ to SQL DeliciousCandyBar ( CandyBarId), CandyBar.

: Linq to SQL ?

(), LINQ to SQL .

UPDATE:

, . . . -.

CandyBar DelciousCandyBar [ (Name= "DeliciousCandyBar_CandyBar", Storage = "_ DeliciousCandyBar", ThisKey = "Id", OtherKey = "CandyBarId", IsForeignKey = true)]

DeliciousCandyBar CandyBar [ (Name= "DeliciousCandyBar_CandyBar", Storage = "_ CandyBar", ThisKey = "CandyBarId", OtherKey = "Id", IsUnique = true, IsForeignKey = false)]

, , .

CandyBar DeliciousCandyBar SQL Management

2

, . SSMS , (CandyBar.Id). . .

+3
2

, linq2sql . , linq2sql, , . , linq2sql.

, , , , .. CandyBar , . , .

, , , , candybar deliciouscandy .

0

:

  • CandyBar - DeliciousCandyBar , SubmitChanges().

  • , ID GUID, - , CandyBarID. CandyBarID DeliciousCandyBar CandyBar.

  • DeliciousCandyBar, CandyBarID , CandyBar.

0

Source: https://habr.com/ru/post/1705720/


All Articles