mapping not working when loaded from test
load from test.... NOT working
Database.SetInitializer(new ParadoxContextInitializerIfModelChanges());
var context = new ParadoxContext();
context.Database.Initialize(false); //force OnModelCreating
Exception:
System.Data.SqlClient.SqlException: 'Column 'DataSourceName' in table 'dbo.DataSources' is of a type that is invalid for use as a key column in an index.'
this is the mapping :
public class DataSourceMap : EntityTypeConfiguration<DataSource>
{
public DataSourceMap()
{
var indexName = "IX_" + nameof(DataSource) + "_MultipleColumns_" + nameof(DataSource.OrgId) + "_And_" + nameof(DataSource.DataSourceName);
this
.Property(e => e.OrgId)
.HasColumnAnnotation(
IndexAnnotation.AnnotationName,
new IndexAnnotation(new IndexAttribute(indexName, 1) { IsUnique = true } ));
this
.Property(e => e.DataSourceName)
.HasColumnAnnotation(
IndexAnnotation.AnnotationName,
new IndexAnnotation(new IndexAttribute(indexName, 2) { IsUnique = true }));
}
}
exactly same code but started from.... Web project no issue.
Database.SetInitializer(new ParadoxContextInitializerIfModelChanges());
var context = new ParadoxContext();
including for completness.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
if (modelBuilder == null) { throw new ArgumentNullException("modelBuilder"); }
////todo make this auto
modelBuilder.Configurations.Add(new AppSettingMap());
modelBuilder.Configurations.Add(new DataSourceMap());
modelBuilder.Configurations.Add(new SourceMap());
modelBuilder.Configurations.Add(new TemplateMap());
modelBuilder.Configurations.Add(new ImportFileMap());
modelBuilder.Configurations.Add(new ImportFileLineMap());
modelBuilder.Configurations.Add(new FtpConnectionMap());
modelBuilder.Configurations.Add(new LookupMap());
modelBuilder.Configurations.Add(new ImportFileProcessLogMap());
modelBuilder.Configurations.Add(new ImportFilePhysicalMap());
}
further commenting out all line like the below from mapping makes that part get further....
this
.Property(e => e.DataSourceName)
.HasColumnAnnotation(
IndexAnnotation.AnnotationName,
new IndexAnnotation(new IndexAttribute(indexName, 2) { IsUnique = true }));
Then it seems to ignore [NotMapped]
[NotMapped]
public override DateTime DateCreated { get => base.DateCreated; set => base.DateCreated = value; }
how does simply making it be created from a test, break this much.....
the fix
adding this to the test project fixed it.
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.0.0.0" />
</dependentAssembly>
Please! someone make this easier to debug and make working in the future its a nightmare! without this it gives incorrect errors!!!
This might be a VS issue with how VS loads dependencies.
If you're using the nuget package System.ComponentModel.Annotations, then make sure it is directly referenced from all the projects using the model project.
I ran into this issue when the model project was using the System.ComponentModel.Annotations nuget package, but my console app was using the one in the GAC.
Your fix works probably because it forces it to look for the correct package version that wont be in the GAC.
This issue has been closed because EF6 is no longer being actively developed. We are instead focusing on stability of the codebase, which means we will only make changes to address security issues. See the repo README for more information.