This project has moved. For the latest updates, please go here.
2
Vote

Complex types issue

description

The insert Statement is correct generated. The inserted data does not contain the vlaues for properties from ComplexType or the values are placed in wrong order.
This lead in my case to an attempt to insert null values in not nullable fields.
Moving the fields from Complex Type into the Model solves the problem.


public class SampleModel
{
...
    public int Prop1 {get;set;}
    public SampleComplexType  properties {get;set;} 
    public int? Prop2 {get;set;}
...
}

piblic class SampleComplexType
{
public long Property1 {get;set;}
public long Property2 {get;set;}
}

public MyContext : DbContext
{
...
    public DbSet<SampleModel> SampleModels { get; set; }
...
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
...
        modelBuilder.ComplexType<SampleModel>().Property(t => t.Property1).HasColumnName("SampleComplexType_p1");
        modelBuilder.ComplexType<SampleModel>().Property(t => t.Property2).HasColumnName("SampleComplexType_p2");
...
    }
...
}



This would work correctly:
public class SampleModel
{
...
    public int Prop1 {get;set;}
    public long SampleComplexType_p1{get;set;}
    public long SampleComplexType_p2{get;set;}
    public int? Prop2 {get;set;}
...
}

comments

xoxoxexe wrote Feb 1, 2016 at 2:04 PM

After Debugging i found out that the described issue occurs due to wrong Mapping Info from EntityFramework.MappingAPI.

wrote Feb 15, 2016 at 4:58 PM

keith9820 wrote Feb 15, 2016 at 9:00 PM

Is there any resolution? My project is hamstrung on the issue and I'd prefer not to flatten out my entity model by doing away with the complex type properties.

xoxoxexe wrote Feb 23, 2016 at 8:28 AM

As a temporary solution i have created mapping profiles (IEnumerable<SqlBulkCopyColumnMapping>) needed for SqlBulkInsert manually, and implemented basic BulkInsert functionality myself. It works like it should.
Next i'm going to implement automatic Mapping Info generation from DbContext just like MappingAPI does.
    using (SqlBulkCopy objBulkCopy = new SqlBulkCopy(_strConnectionString, _options))
        {
            foreach (SqlBulkCopyColumnMapping objMapping in _ColumnMappings)
                objBulkCopy.ColumnMappings.Add(objMapping);
            objBulkCopy.BulkCopyTimeout = _timeout;
            objBulkCopy.DestinationTableName = _storageName;
            objBulkCopy.BatchSize = intBatchSize;

            using (ISourceSettableDataReader<T> objReader = new ObjectDataReader<T>(_objMapper))
            {
                objReader.SetData(list);
                objBulkCopy.WriteToServer(objReader);
            }
        }