-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
No suitable constructor found for entity type 'string' #11074
Comments
@bradmarder Can you provide more details on how you are using this package? For example, which feed are you using? Which .NET Core installer did you run? What are you using to build? |
As for other details, theres a lot. Everything worked with aspnetcore.all 2.05 package, and I changed my target frameworks to |
@bradmarder - Can you share your model details? Do you have entityType named |
Got the same issue. Model:
ComplexType:
DbContext:
Exception: Worked with 2.0 |
Narrowed it down a lot. I removed all db entities. Only left with registering identity stuff.
|
@bradmarder - I am not able to get it to work with modifications you provided. I took MVC web app template and modified ApplicationDbContext as your code and I m hitting error.
It would be useful if you can provide us a sample solution demonstrating the issue or find minimum repro without use of Identity. |
@Alexxtheonly - The model you shared is working fine in 2.1.0-preview1-final using System;
using System.Collections.ObjectModel;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace EFSampleApp
{
public class Program
{
public static void Main(string[] args)
{
using (var db = new MyContext())
{
// Recreate database
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
// Seed database
db.SaveChanges();
}
using (var db = new MyContext())
{
// Run queries
}
Console.WriteLine("Program finished.");
}
}
public class MyContext : DbContext
{
private static ILoggerFactory LoggerFactory => new LoggerFactory().AddConsole(LogLevel.Trace);
// Declare DBSets
public DbSet<TransferAccessProfile> Blogs { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Select 1 provider
optionsBuilder
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=_ModelApp;Trusted_Connection=True;Connect Timeout=5;ConnectRetryCount=0")
//.UseSqlite("filename=_modelApp.db")
//.UseInMemoryDatabase(databaseName: "_modelApp")
.EnableSensitiveDataLogging()
.UseLoggerFactory(LoggerFactory);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Configure model
modelBuilder.Entity<TransferAccessProfile>(e =>
{
e.HasKey(o => o.Id);
e.OwnsOne(o => o.DeviceIds).Property(o => o.Serialized).HasColumnName(nameof(TransferAccessProfile.DeviceIds));
e.OwnsOne(o => o.TimespanIds).Property(o => o.Serialized).HasColumnName(nameof(TransferAccessProfile.TimespanIds));
});
}
}
public class TransferAccessProfile
{
public int Id { get; set; }
public CollectionComplexType<int> DeviceIds { get; set; }
public CollectionComplexType<int> TimespanIds { get; set; }
public int AccessDaysId { get; set; }
}
public class CollectionComplexType<T> : Collection<T>
{
public CollectionComplexType()
{
}
public string Serialized
{
get => "";// JsonConvert.SerializeObject(this);
private set
{
Clear();
if (string.IsNullOrEmpty(value))
{
return;
}
//foreach (var item in JsonConvert.DeserializeObject<T[]>(value))
//{
// Add(item);
//}
}
}
}
} |
@smitpatel |
Thanks for repro code @Alexxtheonly using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.Extensions.Logging;
namespace EFSampleApp
{
public class Program
{
public static void Main(string[] args)
{
using (var db = new MyContext())
{
// Recreate database
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
// Seed database
var test = new DictionaryComplexType<int, string>();
db.Add(new TestModel()
{
Id = 1,
Ints = test,
});
db.SaveChanges();
}
using (var db = new MyContext())
{
// Run queries
}
Console.WriteLine("Program finished.");
}
}
public class MyContext : DbContext
{
private static ILoggerFactory LoggerFactory => new LoggerFactory().AddConsole(LogLevel.Trace);
// Declare DBSets
public DbSet<TestModel> TestModels { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Select 1 provider
optionsBuilder
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=_ModelApp;Trusted_Connection=True;Connect Timeout=5;ConnectRetryCount=0")
//.UseSqlite("filename=_modelApp.db")
//.UseInMemoryDatabase(databaseName: "_modelApp")
.EnableSensitiveDataLogging()
.UseLoggerFactory(LoggerFactory);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Configure model
modelBuilder.Entity<TestModel>(e =>
{
e.HasKey(o => o.Id);
e.OwnsOne(o => o.Ints);
});
Console.WriteLine((modelBuilder.Model as Model)?.ToDebugString());
}
}
public class Blog
{
public int Id { get; set; }
}
public class TestModel
{
public int Id { get; set; }
public DictionaryComplexType<int, string> Ints { get; set; }
}
public class DictionaryComplexType<TKey, TValue> : Dictionary<TKey, TValue>
{
}
} We started discovering properties in |
Assigning to @AndriySvyryd |
Got hit with this also. |
@ajcvickers I just found a very simple repo. Just add Adding the attribute |
Just had another run in with this issue. Curious if it was some sort of default value or constructor issue I set a default value in my modelbuilder.. I switched my System.Uri to string and things are back to operational. |
When does this change become a nuget package? It zinged me yesterday. |
@jholovacs It will be in the nightly builds (listed on the homepage) first--probably already, although I didn't check, and it will get to nuget.org as part of the preview2 release. |
This issue will also arise in inheritance if the inherited doesn't provide a default constructor. Example public class Enumeration<TValue>
{
public TValue Value { get; private set; }
public string Display {get; private set; }
public Enumeration(TValue value, string display) { ... }
protected Enumeration() { /* ORM */ }
}
public class Type : Enumeration<int>
{
public class Type(int id, string display) : base(id, display) { .. }
// This fixes the issue in 2.1.0
protected Type() { /* ORM */ }
} 2.1 prior fix
Stack trace
|
@jholovacs The exception you posted indicates that no matching properties could be found for "id" and "display". This is a completely different error than the one posted for this bug. Also, the code you posted is incomplete and has errors, but when I correct things and attempt to repro I get a different message--it finds the Display property, as expected, but can't match "id" because there is no "Id" property. This is all as expected. If after reconsidering this you believe there is still a bug, then please file a new issue including a runnable project/solution or code listing that reproduces the behavior. |
...huh? I don't believe I posted an exception? |
@jholovacs Sorry! I meant to mention @joacar |
Testing out
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.0-preview1-final" />
and EFCore just throws this. I'm completely lost in how to approach this.Further technical details
EF Core version: Microsoft.AspNetCore.App 2.1.0-preview1-final
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: win10 x64
IDE: Visual Studio 2017 Preview 2 15.6.0 Preview 6.0
The text was updated successfully, but these errors were encountered: