So i have an existing class, which looks like this:
Public Class Song
<Key>
Public Property GUID As Guid
<ForeignKey("Creator")>
Public Property GUID_Creator As Guid
Public Property Creator As User
End Class
This works perfectly, i'm able to add new songs and all that.
Recently, i decided i need to map some things using the Fluent API, but i didn't want to remake my whole model using the API, all i needed was just disabling cascade delete.
So i wandered off to onModelCreating
, to add
modelBuilder.Entity(Of Song) _
.HasRequired(Function(s) s.Creator) _
.WithMany(Function(u) u.Songs) _
.HasForeignKey(Function(s) s.GUID_Creator) _
.WillCascadeOnDelete(False)
I have added just that, the method was empty before. When firing the app after this (i have auto migrations enabled) and using the context, suddenly i get an exception for EVERY SINGLE CLASS in my context, saying:
EntityType 'Song' has no key defined
EntityType 'User' has no key defined
etc. I Googled for people using both Fluent API and Data Annotations at the same time, and found some people that had it working. Adding HasKey
in the Fluent API solves it, but i don't wanna remake every class in OnModelCreating
.
So how do i go about it? Is it possible to mix Fluent API and Data Annotations?