Skip to content
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

The entity type 'System.Uri' requires a key to be defined #3062

Closed
bitcrazed opened this issue Sep 9, 2015 · 8 comments
Closed

The entity type 'System.Uri' requires a key to be defined #3062

bitcrazed opened this issue Sep 9, 2015 · 8 comments

Comments

@bitcrazed
Copy link

Added a URI field to my model (Post.cs).

Adding a migration to record the new field results in The entity type 'System.Uri' requires a key to be defined error message:

> dnx ef migrations add "Added HeaderImage to Post"
Using context 'BlogDbContext'.
Microsoft.Data.Entity.Metadata.ModelItemNotFoundException: The entity type 'System.Uri' requires a key to be defined.
   at Microsoft.Data.Entity.Metadata.EntityTypeExtensions.GetPrimaryKey(IEntityType entityType)
   at Microsoft.Data.Entity.Migrations.Internal.MigrationsModelDiffer.<Add>d__22.MoveNext()
   at Microsoft.Data.Entity.Migrations.Internal.MigrationsModelDiffer.<DiffCollection>d__44`1.MoveNext()
   at System.Linq.Enumerable.<ConcatIterator>d__58`1.MoveNext()
   at System.Linq.Enumerable.<ConcatIterator>d__58`1.MoveNext()
   at System.Linq.Enumerable.<ConcatIterator>d__58`1.MoveNext()
   at Microsoft.Data.Entity.Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable`1 operations, DiffContext diffContext)
   at Microsoft.Data.Entity.Commands.Migrations.MigrationScaffolder.ScaffoldMigration(String migrationName, String rootNamespace)
   at Microsoft.Data.Entity.Commands.MigrationTool.AddMigration(String migrationName, String contextTypeName, String startupAssemblyName, String rootNamespace, String projectDir)
   at Microsoft.Data.Entity.Commands.Program.<>c__DisplayClass11_0.<AddMigration>b__0()
   at Microsoft.Data.Entity.Commands.Program.Execute(String startupProject, Action invoke)
   at Microsoft.Data.Entity.Commands.Program.<>c__DisplayClass9_7.<Main>b__17()
   at Microsoft.Dnx.Runtime.Common.CommandLine.CommandLineApplication.Execute(String[] args)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.Dnx.Runtime.Common.EntryPointExecutor.Execute(Assembly assembly, String[] args, IServiceProvider serviceProvider)
   at Microsoft.Dnx.ApplicationHost.Program.ExecuteMain(DefaultHost host, String applicationName, String[] args)
   at Microsoft.Dnx.ApplicationHost.Program.Main(String[] args)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.Dnx.Runtime.Common.EntryPointExecutor.Execute(Assembly assembly, String[] args, IServiceProvider serviceProvider)
   at Microsoft.Dnx.Host.Bootstrapper.RunAsync(List`1 args, IRuntimeEnvironment env, FrameworkName targetFramework)
   at Microsoft.Dnx.Host.RuntimeBootstrapper.ExecuteAsync(String[] args, FrameworkName targetFramework)
   at Microsoft.Dnx.Host.RuntimeBootstrapper.Execute(String[] args, FrameworkName targetFramework)

Why is this? What am I doing wrong?

https://github.com/bitcrazed/EFTestWebApp

@anpete
Copy link
Contributor

anpete commented Sep 9, 2015

Looks like we are trying to bring it in as an Entity.

I tried it in EF6 and interestingly we only bring it in to CSpace!

@divega
Copy link
Contributor

divega commented Sep 9, 2015

It is missing a parameterless constructor, I think.

@anpete
Copy link
Contributor

anpete commented Sep 9, 2015

See #240 & #246

@divega
Copy link
Contributor

divega commented Sep 9, 2015

See #3034 as well. For the short term at least these are both negative scenarios in which we find a property of a type that cannot be mapped, so we want to throw a helpful exception.

Cc @smitpatel

@bitcrazed
Copy link
Author

@divega No, the Post entity has no defined constructor so default parameterless should be present, no?

What's the work-around for this Declare the Uri property as string? (Hoping I don't have to do this)

@divega
Copy link
Contributor

divega commented Sep 9, 2015

I was referring to System.Uri not having a parameterless constructor. Since System.Uri is not supported as a primitive property type (in either EF7 or EF6) we try to map it as structural object with its own properties.

In EF7 we still don't support complex types so we try to make it an entity type in the model, but soon enough we will lrarn that it does not contain a property that we could automatically pick as the entity key (that is the exception you got).

In EF6 we do support complex types, which don't need a key, so we can go a bit further, but ultimately can't succeed because we would need it to have a parameterless constructor.

Yes the workaround is to map it as a string (you can still have a System.Uri property that wraps the string, and tell EF to ignore it) until the features Andrew pointed out are implemented.

@bitcrazed
Copy link
Author

@divega Got it. Thanks. Will map for now.

@rowanmiller
Copy link
Contributor

I think everything actionable from the EF side is already tracked by other issues. Feel free to reopen if you disagree.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants