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

Entity with an enum propery type may throw an InvalidCastException when using FromSql #3465

Closed
dealdiane opened this issue Oct 17, 2015 · 0 comments
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@dealdiane
Copy link

EF assumes that columns are returned from the database in the order it expects. This is not always true especially if using FromSql.

Suppose you have a db schema:

public enum PetType
{
    Dog,
    Cat
}

public class Pet
{
    public int Id { get; set; }
    public string Name { get; set; }
    public PetType Type { get; set; }
}

public class SampleDb : DbContext
{
    public DbSet<Pet> Pets { get; set; }
}

calling db.Pets.FromSql(@"SELECT Id, [Type], [Name] FROM Pet").ToList(); will throw an InvalidCastException. It assumes that the 3rd column is of type PetType (int).

InvalidCastException: Specified cast is not valid.
lambda_method(Closure , Object[] )
Microsoft.Data.Entity.Storage.Internal.RemappingUntypedRelationalValueBufferFactory.Create(DbDataReader dataReader) in RemappingUntypedRelationalValueBufferFactory.cs
            _processValuesAction?.Invoke(values);
Microsoft.Data.Entity.Query.Internal.QueryingEnumerable.Enumerator.MoveNext() in QueryingEnumerable.cs
                    Current
System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
System.Linq.Enumerable.<SelectManyIterator>d__1`2.MoveNext()
System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
Microsoft.Data.Entity.Query.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext() in LinqOperatorProvider.cs
                        throw;
System.Linq.AsyncEnumerable.<>c__DisplayClass5`1.<>c__DisplayClass7.<ToAsyncEnumerable>b__2()
System.Threading.Tasks.Task`1.InnerInvoke()
System.Threading.Tasks.Task.Execute()
@rowanmiller rowanmiller added this to the 7.0.0 milestone Oct 20, 2015
@mikary mikary closed this as completed in 0030ce0 Nov 24, 2015
@mikary mikary modified the milestones: 7.0.0-rc2, 7.0.0 Nov 24, 2015
@ajcvickers ajcvickers modified the milestones: 1.0.0-rc2, 1.0.0 Oct 15, 2022
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

4 participants