efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Model Change onModelchange

Open GVX111 opened this issue 10 years ago • 2 comments

When i modelBuilder.Model.RemoveEntityType(table.TableType) Entity from modelBuilder and after i creating new entity ( var builder = modelBuilder.Entity(table.TableType).ToTable(table.TableName) .Property(property.PropertyType, property.PropertyName) .IsRequired(property.Required);) on execution query of select making Exception. But if i rerun application working good.

Exception message. Additional information: [A]System.Func`2[Microsoft.Data.Entity.Query.QueryContext,System.Collections.Generic.IEnumerable`1[Employees]] cannot be cast to [B]System.Func`2[Microsoft.Data.Entity.Query.QueryContext,System.Collections.Generic.IEnumerable`1[Employees]]. Type A originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Type B originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'.

   private void GenerateTableModel(ModelBuilder modelBuilder, Table table)
        {
            if (modelBuilder.Model.FindEntityType(table.TableType) != null)
                modelBuilder.Model.RemoveEntityType(table.TableType);

```
        foreach (var property in table.Properties)
        {
            var builder = modelBuilder.Entity(table.TableType).ToTable(table.TableName)
                .Property(property.PropertyType, property.PropertyName)
                .IsRequired(property.Required);
            if (property.IsRename)
            {
                builder.HasColumnName(property.RenameProperty);
            }
            if (property.Key)
            {
                builder.ValueGeneratedOnAdd();
            }
        }
        if (table.Properties.Any(e => e.Key))
        {
            modelBuilder.Entity(table.TableType).HasKey(table.Properties.First(e => e.Key).PropertyName);
        }
    }
```
                                    

GVX111 avatar Dec 18 '15 15:12 GVX111

There is not enough information in this issue for us to reproduce and debug the error. Please provide a complete code listing (or project) that we can run to see the error.

rowanmiller avatar Jan 07 '16 19:01 rowanmiller

It was problem of recreating new object instance. fixed

GVX111 avatar Jan 08 '16 18:01 GVX111