Closed ericoldre closed 3 years ago
Hi @ericoldre
It was removed because the data annotations did not define everthing that fluent configuration had, and therefore you always had both. This also meant that any business logic that wanted to use and process the poco's had to inherit a dependancy on entity framework due to entity framework attributes, which is not desirable.
However I understand that you've got reflection code to spot the index attribute. Do you actually need that specific attribute? Could you use your own marker attribute?
For v2, that code used to be in private void SetupConfig()
...
if (Settings.UseDataAnnotations && Indexes.Any())
{
foreach (var index in Indexes)
{
DataAnnotations.Add(string.Format("Index(@\"{0}\", {1}, IsUnique = {2}, IsClustered = {3})",
index.IndexName,
index.KeyOrdinal,
index.IsUnique ? "true" : "false",
index.IsClustered ? "true" : "false"));
}
}
in v3 it is callled protected override void SetupConfig(Column c)
The attributes are actually in this package, which does not have any dependencies: https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Abstractions/
Thank you that was incredibly helpful! I managed to replicate the old functionality by using a custom Generator.
public class GeneratorEf6WithIndexes : GeneratorEf6
{
public GeneratorEf6WithIndexes(FileManagementService fileManagementService, Type fileManagerType)
: base(fileManagementService, fileManagerType)
{
}
protected override void SetupConfig(Column c)
{
base.SetupConfig(c);
if (c.Indexes != null && c.Indexes.Any())
{
foreach (var index in idxes)
{
// pulled from v2 codebase
string idxText = string.Format("Index(@\"{0}\", {1}, IsUnique = {2}, IsClustered = {3})",
index.IndexName,
index.KeyOrdinal,
index.IsUnique ? "true" : "false",
index.IsClustered ? "true" : "false");
c.Attributes.Add($"[{idxText}]");
}
}
}
}
Then use the custom generator instead of calling GeneratorFactory.Create() (near the end of the *.tt file)
// original code that shipped with .tt file
//var generator = GeneratorFactory.Create(fileManagement, FileManagerFactory.GetFileManagerType());
// instead, create our OWN generator then call Init on it like the Factory.Create method would.
var generator = new GeneratorEf6WithIndexes(fileManagement, FileManagerFactory.GetFileManagerType());
generator.Init(null);
Thank you very much for your help!
We have been using version 2 of the generator to create EF6 classes for some time. We are looking to upgrade our team to use version 3. First step is getting version 3 to create a model as similar to version 2 as possible. It's actually gone quite smoothly with just a couple minor issues. The first of which is. We used to get IndexAttributes (DataAnnotations) added to column definitions in version 2. And example:
Other parts of our system are dependent on being able to use the [Index()] attributes via reflection.
I've been trying to use the UpdateColumn() method to add the attribute back, like this:
However, it appears that the Table object passed to the method does not yet have the Indexs list populated on it yet when the UpdateColumn() function is called. They get added in a later step.
Looking for suggestions on how to retain those same data annotations as we had in version 2. Is there another place we could customize that happens later in the process which would give us a chance to add them to the column's Attributes list? Or some other approach?