Hello,
Consider the following classes:
using System.Diagnostics;
using System.Linq;
using Mindscape.LightSpeed;
using Mindscape.LightSpeed.Linq;
public class Entity1 : Entity<int>
{
#region Relationships
[ReverseAssociation("Target")]
private readonly EntityCollection<Entity2> _e1 = new EntityCollection<Entity2>();
[ReverseAssociation("Source")]
private readonly EntityCollection<Entity2> _e2 = new EntityCollection<Entity2>();
#endregion
#region Properties
[DebuggerNonUserCode]
public EntityCollection<Entity2> E2
{
get
{
return Get(_e2);
}
}
[DebuggerNonUserCode]
public EntityCollection<Entity2> E1
{
get
{
return Get(_e1);
}
}
#endregion
}
public class Entity2 : Entity<int>
{
#region Fields
private int _sourceId;
private int _targetId;
#endregion
#region Field attribute and view names
/// <summary>
/// Identifies the SourceId entity attribute.
/// </summary>
public const string SourceIdField = "SourceId";
/// <summary>
/// Identifies the TargetId entity attribute.
/// </summary>
public const string TargetIdField = "TargetId";
#endregion
#region Relationships
[ReverseAssociation("E2")]
private readonly EntityHolder<Entity1> _source = new EntityHolder<Entity1>();
[ReverseAssociation("E1")]
private readonly EntityHolder<Entity1> _target = new EntityHolder<Entity1>();
#endregion
#region Properties
[DebuggerNonUserCode]
public Entity1 Source
{
get
{
return Get(_source);
}
set
{
Set(_source, value);
}
}
[DebuggerNonUserCode]
public Entity1 Target
{
get
{
return Get(_target);
}
set
{
Set(_target, value);
}
}
/// <summary>
/// Gets or sets the ID for the <see cref="Source" /> property.
/// </summary>
[DebuggerNonUserCode]
public int SourceId
{
get
{
return Get(ref _sourceId, "SourceId");
}
set
{
Set(ref _sourceId, value, "SourceId");
}
}
/// <summary>
/// Gets or sets the ID for the <see cref="Target" /> property.
/// </summary>
[DebuggerNonUserCode]
public int TargetId
{
get
{
return Get(ref _targetId, "TargetId");
}
set
{
Set(ref _targetId, value, "TargetId");
}
}
#endregion
}
[Discriminator(Attribute = "Type", Value = 1)]
public class Entity3 : Entity2
{
}
[Discriminator(Attribute = "Type", Value = 2)]
public class Entity4 : Entity2
{
}
/// <summary>
/// Provides a strong-typed unit of work for working with the LightSpeedModel1 model.
/// </summary>
public class LightSpeedModel1UnitOfWork : UnitOfWork
{
public IQueryable<Entity1> Entity1s
{
get
{
return this.Query<Entity1>();
}
}
public IQueryable<Entity2> Entity2s
{
get
{
return this.Query<Entity2>();
}
}
public IQueryable<Entity3> Entity3s
{
get
{
return this.Query<Entity3>();
}
}
}
An the following program to create the entities:
using System;
using Mindscape.LightSpeed;
internal class Program
{
private static void Main(string[] args)
{
try
{
var context = new LightSpeedContext<LightSpeedModel1UnitOfWork>();
context.DataProvider = DataProvider.MySql5;
context.ConnectionString =
"server=localhost;User Id=BobTheBuilder;database=s;Password=Password123;";
int id1;
int id2;
using (var ouw = context.CreateUnitOfWork())
{
var e1 = new Entity1();
ouw.Add(e1);
var e2 = new Entity1();
ouw.Add(e2);
var e3 = new Entity3();
e3.Target = e2;
e3.Source = e1;
ouw.Add(e3);
var e4 = new Entity4();
e4.Target = e2;
e4.Source = e1;
ouw.Add(e4);
ouw.SaveChanges();
id1 = e3.Id;
id2 = e4.Id;
}
using (var ouw = context.CreateUnitOfWork())
{
var e1 = ouw.FindById<Entity3>(id1);
Console.Out.WriteLine(e1 == null ? "Oops" : "OK");
var e2 = ouw.FindById<Entity4>(id2);
Console.Out.WriteLine(e2 == null ? "Oops" : "OK");
}
}
catch (Exception ex)
{
Console.Out.WriteLine(ex);
}
}
}
The migration looks as follows:
using Mindscape.LightSpeed.Migrations;
[Migration("20120119030032")]
public class V1 : Migration
{
public override void Up()
{
AddKeyTable("KeyTable", null, ModelDataType.Int32, 1);
AddTable("Entity1", new Field("Id", ModelDataType.Int32, false));
AddTable("Entity2", new Field("Id", ModelDataType.Int32, false),
new Field("Type", ModelDataType.Int32, false),
new ForeignKeyField("SourceId", ModelDataType.Int32,
false, "Entity1",
null, "Id"),
new ForeignKeyField("TargetId", ModelDataType.Int32,
false, "Entity1",
null, "Id")
);
}
public override void Down()
{
DropColumn("Entity2", null, "SourceId", true);
DropColumn("Entity2", null, "TargetId", true);
DropTable("Entity2", null);
DropTable("Entity1", null);
}
}
Now when I run the program and look at the MySQL database, the "Type" column on the Entity2 column is 0. If I add a Type
property on type int
on class Entity2
, then the type column has the appropriate values (1 for Entity3 instances and 2 for Entity4 instances).
Is this the desired behavior? I am under the understanding that I do not have to have a property called "Type" on Entity2. It just needs to exist in the database.
Thanks,
Werner