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 2 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
5 changes: 3 additions & 2 deletions EfEnumToLookup/LookupGenerator/EnumReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ namespace EfEnumToLookup.LookupGenerator
[DebuggerDisplay("{DebuggerDisplay,nq}")]
internal class EnumReference
{
public string ReferencingTable { get; set; }
public string ReferencingTable { get; set; }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whitespace error - format the file before committing

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 string.Format("EnumReference: {3}.{0}.{1} ({2})", ReferencingTable, ReferencingField, EnumType.Name, ReferencingSchema); }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-order these for readability, or better still convert to the new interpolated string format. I might do a separate commit to use interpolation everywhere

}
}
}
11 changes: 7 additions & 4 deletions EfEnumToLookup/LookupGenerator/MetadataHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,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;

foreach (var edmProperty in properties)
var schema = mappingFragment.StoreEntitySet.Schema ?? "dbo";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

incorrect indentation - this project uses tabs. Think there's an editorconfig file, if not there should be. http://editorconfig.org/

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defaulting to 'dbo' might cause problems with implementing non-sql-server variants (which have been requested). probably better to leave it unqualified if no schema present

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see issue #44


foreach (var edmProperty in properties)
{
if (edmProperty.IsEnumType)
{
references.Add(new EnumReference
{
ReferencingTable = table,
ReferencingField = GetColumnName(mappingFragment, edmProperty),
ReferencingSchema = schema,
ReferencingField = GetColumnName(mappingFragment, edmProperty),
EnumType = objectItemCollection.GetClrType(edmProperty.EnumType),
});
continue;
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
4 changes: 2 additions & 2 deletions EfEnumToLookup/LookupGenerator/SqlServerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ private string AddForeignKeys(IEnumerable<EnumReference> refs)
var fkName = string.Format("FK_{0}_{1}", enumReference.ReferencingTable, enumReference.ReferencingField);

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 [{4}].[{1}] ADD CONSTRAINT {0} FOREIGN KEY ([{2}]) REFERENCES [{3}] (Id);\r\n",
fkName, enumReference.ReferencingTable, enumReference.ReferencingField, TableName(enumReference.EnumType.Name), enumReference.ReferencingSchema
);
}
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);
}
}

}
}