Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for EF models with different schemas #57

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion EfEnumToLookup/LookupGenerator/EnumReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ internal class EnumReference
{
public string ReferencingTable { get; set; }
public string ReferencingField { get; set; }
public string ReferencingSchema { get; set; }
public Type EnumType { get; set; }

// ReSharper disable once UnusedMember.Local
private string DebuggerDisplay
{
get { return string.Format("EnumReference: {0}.{1} ({2})", ReferencingTable, ReferencingField, EnumType.Name); }
get { return $"EnumReference: {ReferencingSchema}.{ReferencingTable}.{ReferencingField} ({EnumType.Name})"; }
}
}
}
9 changes: 6 additions & 3 deletions EfEnumToLookup/LookupGenerator/MetadataHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,17 @@ private static IEnumerable<EnumReference> ProcessEdmProperties(IEnumerable<EdmPr

// get mapped table name from mapping, or fall-back to just the name if no mapping is set,
// I have no idea what causes Table to be null, and I have no unit test for it yet, but I have seen it.
var table = mappingFragment.StoreEntitySet.Table ?? mappingFragment.StoreEntitySet.Name;

var table = mappingFragment.StoreEntitySet.Table ?? mappingFragment.StoreEntitySet.Name;
var schema = mappingFragment.StoreEntitySet.Schema;

foreach (var edmProperty in properties)
{
if (edmProperty.IsEnumType)
{
references.Add(new EnumReference
{
ReferencingTable = table,
ReferencingSchema = schema,
ReferencingField = GetColumnName(mappingFragment, edmProperty),
EnumType = objectItemCollection.GetClrType(edmProperty.EnumType),
});
Expand All @@ -77,7 +79,8 @@ where nestedProperty.IsEnumType
select new EnumReference
{
ReferencingTable = table,
ReferencingField = GetColumnName(mappingFragment, edmProperty, nestedProperty),
ReferencingSchema = schema,
ReferencingField = GetColumnName(mappingFragment, edmProperty, nestedProperty),
EnumType = objectItemCollection.GetClrType(nestedProperty.EnumType),
});
}
Expand Down
9 changes: 6 additions & 3 deletions EfEnumToLookup/LookupGenerator/SqlServerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,13 @@ private string AddForeignKeys(IEnumerable<EnumReference> refs)
{
var fkName = string.Format("FK_{0}_{1}", enumReference.ReferencingTable, enumReference.ReferencingField);

var schemaPrefix = enumReference.ReferencingSchema == null ? string.Empty : $"{enumReference.ReferencingSchema}.";
var schemaPrefixQuoted = enumReference.ReferencingSchema == null ? string.Empty : $"[{enumReference.ReferencingSchema}].";

sql.AppendFormat(
" IF OBJECT_ID('{0}', 'F') IS NULL ALTER TABLE [{1}] ADD CONSTRAINT {0} FOREIGN KEY ([{2}]) REFERENCES [{3}] (Id);\r\n",
fkName, enumReference.ReferencingTable, enumReference.ReferencingField, TableName(enumReference.EnumType.Name)
);
" IF OBJECT_ID('{4}{0}', 'F') IS NULL ALTER TABLE {5}[{1}] ADD CONSTRAINT {0} FOREIGN KEY ([{2}]) REFERENCES [{3}] (Id);\r\n",
fkName, enumReference.ReferencingTable, enumReference.ReferencingField, TableName(enumReference.EnumType.Name), schemaPrefix, schemaPrefixQuoted
);
}
return sql.ToString();
}
Expand Down
1 change: 1 addition & 0 deletions EfEnumToLookupTests/Model/Rabbit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace EfEnumToLookupTests.Model
{
[Table("Rabbit", Schema = "animals")]
public class Rabbit
{
public int Id { get; set; }
Expand Down
16 changes: 10 additions & 6 deletions EfEnumToLookupTests/Tests/ModelParsingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,22 @@ public void FindsReferences()
{
references = _enumToLookup.FindEnumReferences(context);
}
var legs = references.SingleOrDefault(r => r.ReferencingField == "SpeedyLegs");
var legsWithSchema = references.SingleOrDefault(r => r.ReferencingField == "SpeedyLegs" && r.ReferencingSchema == "animals");
Assert.IsNotNull(legsWithSchema, "SpeedyLegs ref not found");

var legs = references.SingleOrDefault(r => r.ReferencingField == "SpeedyLegs");
Assert.IsNotNull(legs, "SpeedyLegs ref not found");
var ears = references.SingleOrDefault(r => r.ReferencingField == "TehEars");
Assert.IsNotNull(ears, "TehEars ref not found");
var echos = references.SingleOrDefault(r => r.ReferencingField == "EchoType");
Assert.IsNotNull(echos, "EchoType ref not found");
Assert.IsNotNull(echos, "EchoType ref not found");
var eons = references.Count(r => r.EnumType == typeof(Eon));
Assert.AreEqual(2, eons, "Wrong number of Eon refs found");
Assert.IsTrue(references.All(r => r.EnumType.IsEnum), "Non-enum type found");
Assert.AreEqual(13, references.Count);
}

[Test]
}
[Test]
public void FindsEnumOnType()
{
var enums = _enumToLookup.FindEnums(typeof (Rabbit));
Expand All @@ -57,5 +60,6 @@ public void FindsNullableEnumOnType()
Assert.IsNotNull(prop, "Enum property not found");
Assert.AreEqual(typeof (Legs?), prop.PropertyType);
}
}

}
}