Created
January 26, 2012 12:55
-
-
Save chalup/1682648 to your computer and use it in GitHub Desktop.
Master-detail relation in LINQ to SQL for Windows Phone 7.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.ComponentModel; | |
using System.Data.Linq; | |
using System.Data.Linq.Mapping; | |
using System.Linq; | |
using Microsoft.Silverlight.Testing; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
namespace blah | |
{ | |
public class TestDb : DataContext | |
{ | |
public TestDb(string connectionString) : base(connectionString) | |
{ } | |
public Table<Master> Masters; | |
public Table<Detail> Details; | |
} | |
[Table] | |
public class Master | |
{ | |
private int _id; | |
[Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)] | |
public int Id | |
{ | |
get { return _id; } | |
set { _id = value; } | |
} | |
private string _title; | |
[Column(CanBeNull = false)] | |
public string Title | |
{ | |
get { return _title; } | |
set { _title = value; } | |
} | |
private EntitySet<Detail> _details; | |
[Association(Storage = "_details", OtherKey = "_masterId", ThisKey = "Id", DeleteRule = "CASCADE")] | |
public EntitySet<Detail> Details | |
{ | |
get { return _details; } | |
set { _details.Assign(value); } | |
} | |
public Master() | |
{ | |
_details = new EntitySet<Detail>( | |
detail => detail.Master = this, | |
detail => detail.Master = null); | |
} | |
} | |
[Table] | |
public class Detail | |
{ | |
private int _id; | |
[Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)] | |
public int Id | |
{ | |
get { return _id; } | |
set { _id = value; } | |
} | |
private string _title; | |
[Column(CanBeNull = false)] | |
public string Title | |
{ | |
get { return _title; } | |
set { _title = value; } | |
} | |
[Column(CanBeNull = false)] | |
internal int _masterId; | |
private EntityRef<Master> _master; | |
[Association(Storage = "_master", ThisKey = "_masterId", OtherKey = "Id", IsForeignKey = true, DeleteOnNull = true)] | |
public Master Master | |
{ | |
get { return _master.Entity; } | |
set | |
{ | |
Master previousValue = _master.Entity; | |
if ((previousValue != value) || (_master.HasLoadedOrAssignedValue == false)) | |
{ | |
if (previousValue != null) | |
{ | |
_master.Entity = null; | |
previousValue.Details.Remove(this); | |
} | |
_master.Entity = value; | |
if (value != null) | |
{ | |
value.Details.Add(this); | |
_masterId = value.Id; | |
} | |
} | |
} | |
} | |
} | |
[TestClass] | |
public class UnitTests : SilverlightTest | |
{ | |
private readonly String KDBConnectionString = "Data Source=isostore:/testdb.sdf"; | |
[TestMethod] | |
[Microsoft.VisualStudio.TestTools.UnitTesting.Description("TestDb")] | |
public void TestDb() | |
{ | |
using (TestDb db = new TestDb(KDBConnectionString)) | |
{ | |
if (db.DatabaseExists()) | |
{ | |
db.DeleteDatabase(); | |
} | |
db.CreateDatabase(); | |
Assert.IsTrue(db.DatabaseExists(), "Setup failed"); | |
db.Masters.InsertOnSubmit(new Master() { Title = "M" }); | |
db.SubmitChanges(); | |
Assert.IsTrue(db.Masters.Count() == 1, "No master created"); | |
db.Details.InsertOnSubmit(new Detail() { Title = "D1", Master = db.Masters.First() }); | |
db.SubmitChanges(); | |
Assert.IsTrue(db.Details.Count() == 1, "No details created"); | |
Assert.IsTrue(db.Masters.First().Details.Count() == 1, "No detail in master"); | |
Assert.IsTrue(db.Masters.First() == db.Details.First().Master, "Master mismatch"); | |
db.Masters.First().Details.Add(new Detail() { Title = "D2" }); | |
db.SubmitChanges(); | |
Assert.IsTrue(db.Details.Count() == 2, "2nd detail not created"); | |
Assert.IsTrue(db.Masters.First().Details.Count() == 2, "2nd detail not added to master"); | |
db.Masters.DeleteOnSubmit(db.Masters.First()); | |
db.SubmitChanges(); | |
Assert.IsTrue(db.Masters.Count() == 0, "Master delete failed"); | |
Assert.IsTrue(db.Details.Count() == 0, "Cascaded detail delete failed"); | |
db.DeleteDatabase(); | |
Assert.IsFalse(db.DatabaseExists(), "Cleanup failed"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment