using System;
using System.Collections.Specialized;
using DOProject1.Model;
using Xtensive.Storage;
using Xtensive.Storage.Configuration;
namespace DOProject1
{
class Program
{
static void Main(string[] args)
{
using (Session session = Session.Open(Domain.Build(DomainConfiguration.Load("Default"))))
{
DisconnectedState disconnectedState = new DisconnectedState();
disconnectedState.Attach(session);
Person person = new Person();
person.Z_LastNameList.CollectionChanged += LastNameListCollectionChanged;
LastName lastName1 = new LastName();
lastName1.Z_Person = person;
System.Diagnostics.Debug.Assert(person.Z_LastNameList.Count == 1, "LastNameList count == 1");
LastName lastName2 = new LastName();
person.Z_LastNameList.Add(lastName2);
System.Diagnostics.Debug.Assert(person.Z_LastNameList.Count == 2, "LastNameList count == 2");
lastName1.Remove();
System.Diagnostics.Debug.Assert(person.Z_LastNameList.Count == 1, "LastNameList count == 1");
person.Z_LastNameList.Remove(lastName2);
System.Diagnostics.Debug.Assert(person.Z_LastNameList.Count == 0, "LastNameList count == 0");
}
Console.WriteLine("Test is finished");
Console.ReadKey();
}
static void LastNameListCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
EntitySet<LastName> set = (EntitySet<LastName>)sender;
switch (e.Action)
{
case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
System.Diagnostics.Debug.Assert(set.Contains((LastName)e.NewItems[0]), "set contains newitem");
break;
case System.Collections.Specialized.NotifyCollectionChangedAction.Move:
break;
case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
// The Contains() of the set throws an 'Entity is removed' Exception?
foreach (var item in set)
{
if (object.ReferenceEquals(item, e.OldItems[0]))
{
System.Diagnostics.Debug.Assert(false, "set not contains newitem");
}
}
break;
case System.Collections.Specialized.NotifyCollectionChangedAction.Replace:
break;
case System.Collections.Specialized.NotifyCollectionChangedAction.Reset:
break;
default:
break;
}
}
}
}
using System;
using Xtensive.Core.IoC;
using Xtensive.Storage;
using Xtensive.Storage.Configuration;
namespace DOProject1.Model
{
[Service(typeof(KeyGenerator), Name = "AbstractEntity")]
public class AbstractEntityKeyGenerator : CachingKeyGenerator<int>
{
[ServiceConstructor]
public AbstractEntityKeyGenerator(DomainConfiguration configuration)
: base(configuration)
{
}
}
[Serializable]
[HierarchyRoot, KeyGenerator(Name = "AbstractEntity")]
public abstract partial class AbstractEntity : Entity
{ // START partial class
#region DataObjectsFields
[Key]
[Field(Nullable = false), FieldMapping("Id")]
public int Z_Id { get; private set; }
[Field(Nullable = false), FieldMapping("CreatedOn")]
public DateTime Z_CreatedOn { get; set; }
[Field(Nullable = false), FieldMapping("CreatedBy")]
public int Z_CreatedBy { get; set; }
[Field(Nullable = false), FieldMapping("ChangedOn")]
public DateTime Z_ChangedOn { get; set; }
[Field(Nullable = false), FieldMapping("ChangedBy")]
public int Z_ChangedBy { get; set; }
[Field(Nullable = false), FieldMapping("IsDefault")]
public bool Z_IsDefault { get; set; }
#endregion //END region DataObjectsFields
}
[Serializable]
public partial class LastName : AbstractEntity
{ // START partial class
#region DataObjectsFields
[Field(Nullable = true), FieldMapping("Name")]
public string Z_Name { get; set; }
[Field(Nullable = false), FieldMapping("PersonId")]
[Association(OnOwnerRemove = OnRemoveAction.Clear, OnTargetRemove = OnRemoveAction.Clear)]
public Person Z_Person { get; set; }
#endregion //END region DataObjectsFields
}
[Serializable]
public partial class Person : AbstractEntity
{ // START partial class
#region DataObjectsFields
[Field(Nullable = true), FieldMapping("PersonnelNumber")]
public string Z_PersonnelNumber { get; set; }
[Field(Nullable = true), FieldMapping("Nickname")]
public string Z_Nickname { get; set; }
[Field(Nullable = true), FieldMapping("FirstNames")]
public string Z_FirstNames { get; set; }
[Field(Nullable = true), FieldMapping("BirthDate")]
public DateTime? Z_BirthDate { get; set; }
[Field(Nullable = true), FieldMapping("DeathDate")]
public DateTime? Z_DeathDate { get; set; }
[Field(Nullable = true), FieldMapping("AHVNumberOld")]
public string Z_AHVNumberOld { get; set; }
[Field(Nullable = true), FieldMapping("AHVNumberNew")]
public string Z_AHVNumberNew { get; set; }
[Field(Nullable = true), FieldMapping("Title")]
public string Z_Title { get; set; }
[Field(Nullable = false), FieldMapping("IsUnknown")]
public bool Z_IsUnknown { get; set; }
[Field(Nullable = false), FieldMapping("WantsToStayAnonymous")]
public bool Z_WantsToStayAnonymous { get; set; }
[Field(Nullable = false), FieldMapping("IsUser")]
public bool Z_IsUser { get; set; }
[Field(Nullable = true), FieldMapping("WindowsLogonName")]
public string Z_WindowsLogonName { get; set; }
[Field(Nullable = true), FieldMapping("UILanguage")]
public string Z_UILanguage { get; set; }
[Field(LazyLoad = true)]
[Association(OnOwnerRemove = OnRemoveAction.Clear, OnTargetRemove = OnRemoveAction.Clear, PairTo = "Z_Person")]
public EntitySet<LastName> Z_LastNameList { get; set; }
#endregion //END region DataObjectsFields
}
}
answered
Nov 20 '10 at 03:29
Paul Sinnema
261●88●88●96