efcore icon indicating copy to clipboard operation
efcore copied to clipboard

NotMappedAttribute Not Respected

Open kscott5 opened this issue 11 years ago • 3 comments

Currently, I've confirmed that NotMappedAttribute isn't working at least with my kre version. This would be helpful if this attribute is support. It would allow additional properties on the ApplicationUser class that inherits from the IdentityUser and prevent those properties from being represented on data model during the EF migration process. Furthermore, I could eliminate the unnecessary MVC model classes (Login, Registration, Profile, etc) and use a single class.

NOTE:

How are classes that extend the Attribute class recognized during the compilation and execution of a .NET application? How does the compiler handle this? How does EF handle this?

PS C:\apps\pcsc\GreenShelter\src\mvc> kvm list

Active Version           Runtime Architecture Location                    Alias
------ -------           ------- ------------ --------                    -----
  *    1.0.0-beta3-11030 clr     x86          C:\Users\kscott\.k\runtimes default
    [Table("AspNetUsers")]
    public class ApplicationUser : IdentityUser<int>   {
        public ApplicationUser() : base() {
            Addresses = new List<Address>();
            Organizations = new List<Organization>();
        }

        [NotMapped]
        public virtual string Password {get; set;}  

        [NotMapped]
        [Compare("Password")]
        public virtual string ConfirmedPassword {get; set;}

        [NotMapped]
        public virtual bool RememberMe {get; set;}

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public override int Id {get; set;}

        public virtual string GuidId {get; set;}
        }
}

k ef migration add NotMappedFields -c MyDbContext Generates migrations for the fields I want to exclude. This tells me that compiler is ignoring the NotMappedAttribute.

kpm build executes without any build errors, but running k web confirms my suspensions and produces the following out:

PS C:\apps\pcsc\GreenShelter\src\mvc> k web
[INFO:GreenShelterLogger] Initializing the database asynchronously
[INFORMATION:GreenShelterLogger] Initializing the database asynchronously
[INFORMATION:GreenShelterLogger] Initializing the database asynchronously
[INFO:GreenShelterLogger] Sql Server Database ensured created
[INFORMATION:GreenShelterLogger] Sql Server Database ensured created
[INFORMATION:GreenShelterLogger] Sql Server Database ensured created
[INFO:GreenShelterLogger] Creating the Application Roles asynchronously
[INFORMATION:GreenShelterLogger] Creating the Application Roles asynchronously
[INFORMATION:GreenShelterLogger] Creating the Application Roles asynchronously
[INFORMATION:Microsoft.Data.Entity.Query.EntityQueryExecutor] Compiling query model: 'from ApplicationRole r in value(Mi
crosoft.Data.Entity.Query.EntityQueryable`1[PCSC.GreenShelter.Models.ApplicationRole]) where ([r].NormalizedName == "ADM
INISTRATOR") select [r] => FirstOrDefault()'
[INFORMATION:Microsoft.Data.Entity.Storage.DataStore] Optimized query model: 'from ApplicationRole r in value(Microsoft.
Data.Entity.Query.EntityQueryable`1[PCSC.GreenShelter.Models.ApplicationRole]) where ([r].NormalizedName == "ADMINISTRAT
OR") select [r] => FirstOrDefault()'
[INFORMATION:Microsoft.Data.Entity.Storage.DataStore] Tracking query sources: [r]
[INFORMATION:Microsoft.Data.Entity.Storage.DataStore] Compiled query expression.
[INFORMATION:Microsoft.Data.Entity.Query.EntityQueryExecutor] Compiling query model: 'from ApplicationRole r in value(Mi
crosoft.Data.Entity.Query.EntityQueryable`1[PCSC.GreenShelter.Models.ApplicationRole]) where ([r].NormalizedName == "ORG
ANIZATION") select [r] => FirstOrDefault()'
[INFORMATION:Microsoft.Data.Entity.Storage.DataStore] Optimized query model: 'from ApplicationRole r in value(Microsoft.
Data.Entity.Query.EntityQueryable`1[PCSC.GreenShelter.Models.ApplicationRole]) where ([r].NormalizedName == "ORGANIZATIO
N") select [r] => FirstOrDefault()'
[INFORMATION:Microsoft.Data.Entity.Storage.DataStore] Tracking query sources: [r]
[INFORMATION:Microsoft.Data.Entity.Storage.DataStore] Compiled query expression.
[INFORMATION:Microsoft.Data.Entity.Query.EntityQueryExecutor] Compiling query model: 'from ApplicationRole r in value(Mi
crosoft.Data.Entity.Query.EntityQueryable`1[PCSC.GreenShelter.Models.ApplicationRole]) where ([r].NormalizedName == "VOL
UNTEER") select [r] => FirstOrDefault()'
[INFORMATION:Microsoft.Data.Entity.Storage.DataStore] Optimized query model: 'from ApplicationRole r in value(Microsoft.
Data.Entity.Query.EntityQueryable`1[PCSC.GreenShelter.Models.ApplicationRole]) where ([r].NormalizedName == "VOLUNTEER")
 select [r] => FirstOrDefault()'
[INFORMATION:Microsoft.Data.Entity.Storage.DataStore] Tracking query sources: [r]
[INFORMATION:Microsoft.Data.Entity.Storage.DataStore] Compiled query expression.
[INFORMATION:Microsoft.Data.Entity.Query.EntityQueryExecutor] Compiling query model: 'from ApplicationRole r in value(Mi
crosoft.Data.Entity.Query.EntityQueryable`1[PCSC.GreenShelter.Models.ApplicationRole]) where ([r].NormalizedName == "CLI
ENT") select [r] => FirstOrDefault()'
[INFORMATION:Microsoft.Data.Entity.Storage.DataStore] Optimized query model: 'from ApplicationRole r in value(Microsoft.
Data.Entity.Query.EntityQueryable`1[PCSC.GreenShelter.Models.ApplicationRole]) where ([r].NormalizedName == "CLIENT") se
lect [r] => FirstOrDefault()'
[INFORMATION:Microsoft.Data.Entity.Storage.DataStore] Tracking query sources: [r]
[INFORMATION:Microsoft.Data.Entity.Storage.DataStore] Compiled query expression.
[INFO:GreenShelterLogger] Creating the Administrator User asynchronously
[INFORMATION:GreenShelterLogger] Creating the Administrator User asynchronously
[INFORMATION:GreenShelterLogger] Creating the Administrator User asynchronously
[INFORMATION:Microsoft.Data.Entity.Query.EntityQueryExecutor] Compiling query model: 'from ApplicationUser u in value(Mi
crosoft.Data.Entity.Query.EntityQueryable`1[PCSC.GreenShelter.Models.ApplicationUser]) where ([u].NormalizedUserName ==
"[email protected]") select [u] => FirstOrDefault()'
[INFORMATION:Microsoft.Data.Entity.Storage.DataStore] Optimized query model: 'from ApplicationUser u in value(Microsoft.
Data.Entity.Query.EntityQueryable`1[PCSC.GreenShelter.Models.ApplicationUser]) where ([u].NormalizedUserName == "ADMIN@G
REENSHELTER.COM") select [u] => FirstOrDefault()'
[INFORMATION:Microsoft.Data.Entity.Storage.DataStore] Tracking query sources: [u]
[INFORMATION:Microsoft.Data.Entity.Storage.DataStore] Compiled query expression.
[ERROR:Microsoft.Data.Entity.Query.EntityQueryExecutor] An exception occurred in the data store while iterating the resu
lts of a query.
System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or more errors occurred. ---
> System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or more errors occurred. -
--> System.Data.SqlClient.SqlException: Invalid column name 'ConfirmedPassword'.
Invalid column name 'Password'.
Invalid column name 'RememberMe'.
   at System.Data.SqlClient.SqlCommand.<ExecuteDbDataReaderAsync>b__24(Task`1 result)
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<InitializeAndReadAsync>d__7.Move
Next()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<MoveNext>d__6.MoveNext()
   --- End of inner exception stack trace ---
   --- End of inner exception stack trace ---
   --- End of inner exception stack trace ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Threading.Tasks.TaskExtensions.CultureAwaiter`1.GetResult()
   at Microsoft.Data.Entity.Query.TaskResultAsyncEnumerable`1.Enumerator.<MoveNext>d__3.MoveNext()
   --- End of inner exception stack trace ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Data.Entity.Query.EntityQueryExecutor.AsyncEnumerableExceptionInterceptor`1.AsyncEnumeratorExceptionInte
rceptor.<MoveNext>d__6.MoveNext()
---> (Inner Exception #0) System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or
 more errors occurred. ---> System.AggregateException: One or more errors occurred. ---> System.Data.SqlClient.SqlExcept
ion: Invalid column name 'ConfirmedPassword'.
Invalid column name 'Password'.
Invalid column name 'RememberMe'.
   at System.Data.SqlClient.SqlCommand.<ExecuteDbDataReaderAsync>b__24(Task`1 result)
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<InitializeAndReadAsync>d__7.Move
Next()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<MoveNext>d__6.MoveNext()
   --- End of inner exception stack trace ---
   --- End of inner exception stack trace ---
   --- End of inner exception stack trace ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Threading.Tasks.TaskExtensions.CultureAwaiter`1.GetResult()
   at Microsoft.Data.Entity.Query.TaskResultAsyncEnumerable`1.Enumerator.<MoveNext>d__3.MoveNext()
---> (Inner Exception #0) System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or
 more errors occurred. ---> System.Data.SqlClient.SqlException: Invalid column name 'ConfirmedPassword'.
Invalid column name 'Password'.
Invalid column name 'RememberMe'.
   at System.Data.SqlClient.SqlCommand.<ExecuteDbDataReaderAsync>b__24(Task`1 result)
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<InitializeAndReadAsync>d__7.Move
Next()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<MoveNext>d__6.MoveNext()
   --- End of inner exception stack trace ---
   --- End of inner exception stack trace ---
---> (Inner Exception #0) System.AggregateException: One or more errors occurred. ---> System.Data.SqlClient.SqlExceptio
n: Invalid column name 'ConfirmedPassword'.
Invalid column name 'Password'.
Invalid column name 'RememberMe'.
   at System.Data.SqlClient.SqlCommand.<ExecuteDbDataReaderAsync>b__24(Task`1 result)
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<InitializeAndReadAsync>d__7.Move
Next()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<MoveNext>d__6.MoveNext()
   --- End of inner exception stack trace ---
---> (Inner Exception #0) System.Data.SqlClient.SqlException (0x80131904): Invalid column name 'ConfirmedPassword'.
Invalid column name 'Password'.
Invalid column name 'RememberMe'.
   at System.Data.SqlClient.SqlCommand.<ExecuteDbDataReaderAsync>b__24(Task`1 result)
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<InitializeAndReadAsync>d__7.Move
Next()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<MoveNext>d__6.MoveNext()
ClientConnectionId:ef4709cb-8cd7-44dc-9c4c-e9c9a08c4d07
Error Number:207,State:1,Class:16<---
<---
<---
<---

System.Data.SqlClient.SqlException (0x80131904): Invalid column name 'ConfirmedPassword'.
Invalid column name 'Password'.
Invalid column name 'RememberMe'.
   at System.Data.SqlClient.SqlCommand.<ExecuteDbDataReaderAsync>b__24(Task`1 result)
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<InitializeAndReadAsync>d__7.Move
Next()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Data.Entity.Relational.Query.AsyncQueryingEnumerable`1.AsyncEnumerator.<MoveNext>d__6.MoveNext()
ClientConnectionId:ef4709cb-8cd7-44dc-9c4c-e9c9a08c4d07
Error Number:207,State:1,Class:16
PS C:\apps\pcsc\GreenShelter\src\mvc>

kscott5 avatar Mar 05 '15 17:03 kscott5

Data annotation support isn't implemented in EF yet, you can track the work here https://github.com/aspnet/EntityFramework/issues/107

rowanmiller avatar Mar 05 '15 17:03 rowanmiller

Rowan, it great that you point me to an existing issue. However, the details there are vague and only state that not all Data Annotation Attributes won't be supported, and never discusses which attributes are.

From reviewing the code, I've determined that InternalEntityBuilder.cs could be the class the supports the following attributes: Key, ForeignKey, PrimaryKey, and Ignore to name a few. These are related to Conventional Linq expression.

What's interesting is that this ConfigurationSource also supports Annotations. So, what is supported and what isn't.

Note: In the future, it would be help to point to the existing issue and give details regarding what is support and possible examples or how a feature is used cause some of the sample projects are dated and doesn't necessarily reflect what is currently being released.

Let's not reinvent the wheel. This include wasting time trying to determine what is and isn't support.

BTW: There's also the TypedAnnotation.cs

kscott5 avatar Mar 05 '15 19:03 kscott5

Hey,

NotMapped will be supported. That note is more about us revisiting what is supported since not everything may make sense in EF7 - no specific decisions have been made yet.

Annotation support will be implemented as conventions.

~Rowan

rowanmiller avatar Mar 05 '15 20:03 rowanmiller