I was creating a content managment system on ABP using entityframework and .net core. I am getting concurrency exccetion when using InsertOrUpdateAsync. I do not have any data in my table.
Please find the Model used to create table.
[Table("CMSContents")]
public class CMSContent:Entity<int>
{
public const int MAXTITLELENGHT = 128;
public const int MAXCONTENTLENGTH = 10000;
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public override int Id { get; set; }
/// <summary>
/// The title of the content.
/// </summary>
[Required]
[StringLength(MAXTITLELENGHT)]
public virtual string PageName { get; set; }
/// <summary>
/// The Cms Content
/// </summary>
public virtual string PageContent { get; set; }
protected CMSContent()
{
}
public static CMSContent CreateContent(int id,string title ,string contents)
{
var #content = new CMSContent
{
Id = id,
PageName = title,
PageContent = contents
};
return #content;
}
}
}
Below is the Application service used to call the repository.
public async Task<CMSContent> InsertOrUpdateCMSContent(CreateContentInput input)
{
var #content = CMSContent.CreateContent(input.Id,input.PageName, input.PageContent);
return await _contentManager.InsertOrUpdateAsync(#content);
}
Exception while calling this API from Swagger,
Mvc.ExceptionHandling.AbpExceptionFilter - Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions.
Abp.Domain.Uow.AbpDbConcurrencyException: Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions. ---> Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions.
at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ThrowAggregateUpdateConcurrencyException(Int32 commandIndex, Int32 expectedRowsAffected, Int32 rowsAffected)
at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ConsumeResultSetWithoutPropagationAsync(Int32 commandIndex, RelationalDataReader reader, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ConsumeAsync(RelationalDataReader reader, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(DbContext _, ValueTuple2 parameters, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func4 operation, Func4 verifySucceeded, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(IReadOnlyList1 entriesToSave, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
at Abp.EntityFrameworkCore.AbpDbContext.SaveChangesAsync(CancellationToken cancellationToken) in D:\Github\aspnetboilerplate\src\Abp.EntityFrameworkCore\EntityFrameworkCore\AbpDbContext.cs:line 224
--- End of inner exception stack trace ---
at Abp.EntityFrameworkCore.AbpDbContext.SaveChangesAsync(CancellationToken cancellationToken) in D:\Github\aspnetboilerplate\src\Abp.EntityFrameworkCore\EntityFrameworkCore\AbpDbContext.cs:line 230
at Abp.Zero.EntityFrameworkCore.AbpZeroCommonDbContext`3.SaveChangesAsync(CancellationToken cancellationToken) in D:\Github\aspnetboilerplate\src\Abp.ZeroCore.EntityFrameworkCore\Zero\EntityFrameworkCore\AbpZeroCommonDbContext.cs:line 170
at Abp.EntityFrameworkCore.Uow.EfCoreUnitOfWork.SaveChangesInDbContextAsync(DbContext dbContext) in D:\Github\aspnetboilerplate\src\Abp.EntityFrameworkCore\EntityFrameworkCore\Uow\EfCoreUnitOfWork.cs:line 167
at Abp.EntityFrameworkCore.Uow.EfCoreUnitOfWork.SaveChangesAsync() in D:\Github\aspnetboilerplate\src\Abp.EntityFrameworkCore\EntityFrameworkCore\Uow\EfCoreUnitOfWork.cs:line 68
at Abp.EntityFrameworkCore.Uow.EfCoreUnitOfWork.CompleteUowAsync() in D:\Github\aspnetboilerplate\src\Abp.EntityFrameworkCore\EntityFrameworkCore\Uow\EfCoreUnitOfWork.cs:line 83
at Abp.Domain.Uow.UnitOfWorkBase.CompleteAsync() in D:\Github\aspnetboilerplate\src\Abp\Domain\Uow\UnitOfWorkBase.cs:line 273
at Abp.AspNetCore.Mvc.Uow.AbpUowActionFilter.OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) in D:\Github\aspnetboilerplate\src\Abp.AspNetCore\AspNetCore\Mvc\Uow\AbpUowActionFilter.cs:line 51
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
I have tried the following approach to make it work, posting as an answer since comment wont allow this many characters.
public async Task<CMSContent> InsertOrUpdateCMSContent(CreateContentInput input)
{
var exists = await _contentRepository
.GetAll()
.AnyAsync(e => e.Id == input.Id);
if (!exists)
{
var #content = CMSContent.CreateContent(input.PageName, input.PageContent);
return await _contentRepository.InsertAsync(#content);
}
else
{
var #content = CMSContent.CreateContent(input.Id, input.PageName, input.PageContent);
return await _contentRepository.UpdateAsync(#content);
}
}
Related
I have some simple models in EF Core (with a Postgres backend using Npgsql).
public class Test : Model
{
public string Id { get; set; }
public TestInfo Info { get; set; }
public string Name { get; set; }
}
public class TestInfo
{
public string[] Tags { get; set; }
public string Name { get; set; }
public int[] Counts { get; set; }
}
public class TestDTO
{
public string[] Tags { get; set; }
public string Name { get; set; }
public string InfoName{ get; set; }
public int[] Counts { get; set; }
}
The EF Core config and Autommaper profile are
public class TestConfig
{
public override void Configure(EntityTypeBuilder<Test> builder)
{
builder.HasKey(m => m.Id);
builder.Property(s => s.Info)
.HasConversion(
v => JsonConvert.SerializeObject(v),
v => JsonConvert.DeserializeObject<TestInfo>(v)
)
.HasColumnType("jsonb");
builder.HasIndex(s => s.Info)
.HasMethod("gin")
.HasOperators("jsonb_ops");
}
}
public class TestProfile : Profile
{
public TestProfile()
{
CreateMap<Test, TestDTO>()
.ForMember(dest => dest.InfoName, opt => opt.MapFrom(src => src.Info.Name))
.ForMember(dest => dest.Tags, opt => opt.MapFrom(src => src.Info.Tags))
.ForMember(dest => dest.Counts, opt => opt.MapFrom(src => src.Info.Counts))
.ReverseMap();
}
}
I have been trying to use Automapper's ProjectTo to query but unable to figure out how to make it work. The below controller code throws coercion errors
[HttpGet]
[Route("/test/{id}")]
public async Task<IActionResult> GetTest([FromRoute (Name = "id")][Required]string id)
{
var t = await _mapper.ProjectTo<TestDTO>(
_context.Tests.Where(s => s.Id == id)
)
.ToListAsync();
return Ok(t);
}
Errors (there is another error from System.String to System.string[] for Tags but I left out for brevity)
System.InvalidOperationException: No coercion operator is defined between types 'System.String' and 'System.Int32[]'.
at System.Linq.Expressions.Expression.GetUserDefinedCoercionOrThrow(ExpressionType coercionType, Expression expression, Type convertToType)
at System.Linq.Expressions.Expression.Convert(Expression expression, Type type, MethodInfo method)
at System.Linq.Expressions.Expression.Convert(Expression expression, Type type)
at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.CreateGetValueExpression(ParameterExpression dbDataReader, Int32 index, Boolean nullable, RelationalTypeMapping typeMapping, Type type, IPropertyBase property)
at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.VisitExtension(Expression extensionExpression)
at System.Linq.Expressions.Expression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at System.Linq.Expressions.ExpressionVisitor.VisitMemberAssignment(MemberAssignment node)
at System.Linq.Expressions.ExpressionVisitor.VisitMemberBinding(MemberBinding node)
at System.Linq.Expressions.ExpressionVisitor.Visit[T](ReadOnlyCollection`1 nodes, Func`2 elementVisitor)
at System.Linq.Expressions.ExpressionVisitor.VisitMemberInit(MemberInitExpression node)
at System.Linq.Expressions.MemberInitExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.ProcessShaper(Expression shaperExpression, RelationalCommandCache& relationalCommandCache, LambdaExpression& relatedDataLoaders)
at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.VisitShapedQuery(ShapedQueryExpression shapedQueryExpression)
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.VisitExtension(Expression extensionExpression)
at System.Linq.Expressions.Expression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass12_0`1.<ExecuteAsync>b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetAsyncEnumerator(CancellationToken cancellationToken)
at System.Runtime.CompilerServices.ConfiguredCancelableAsyncEnumerable`1.GetAsyncEnumerator()
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
at dms_apis.Controllers.OrganisationController.GetTest(String id) in /home/viet/src/dms-apis/Controllers/OrganisationController.cs:line 62
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
I tried manual mapping using Select in the controller and that appears to work but ProjectTo seems to be much nicer
[HttpGet]
[Route("/test/{id}")]
public async Task<IActionResult> GetTest([FromRoute (Name = "id")][Required]string id)
{
var t = _context.Tests
.Select(s => new TestDTO{
Id = s.Id,
Name = s.Info.Name
})
.Where(s => s.Id == id)
.ToList();
return Ok(t);
}
What is the correct way to use ProjectTo in this case? Thanks a lot.
I'm getting the following error trying to load the results of my Postgres function into a EF entity.
Error:
Microsoft.EntityFrameworkCore.Query: Error: An exception occurred while iterating over the results of a query for context type 'Rainman.Data.RainmanDbContext'.
System.Threading.ThreadAbortException: System error.
at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior)
at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader()
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
at Microsoft.EntityFrameworkCore.Query.Internal.FromSqlQueryingEnumerable`1.Enumerator.InitializeReader(DbContext _, Boolean result)
at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
at Microsoft.EntityFrameworkCore.Query.Internal.FromSqlQueryingEnumerable`1.Enumerator.MoveNext()
While running the following code:
FormattableString query = $"SELECT sortedsetkey as \"SortedSetKey\", keyorder as \"KeyOrder\", value as \"Value\" FROM public.fn_test_lcr_custom({routePlan.DeckApplyCustomer.tg_id}, {routePlan.effective_date});";
Console.WriteLine($"\n\nQUERY:\n{query}\n\n");
IEnumerable <CustomerRoutingCache> tmpList = RainmanDbContext.CustomerRoutingCaches.FromSqlInterpolated(query).AsEnumerable();
This SQL Function Definition for the function being called on the Postgres side (ver 13):
CREATE OR REPLACE FUNCTION public.fn_test_lcr_custom(
p_tg_id text,
p_as_of_date timestamp with time zone)
RETURNS TABLE(SortedSetKey text, KeyOrder int, Value text)
The class that it being translated into is:
public class CustomerRoutingCache
{
[Key]
public string SortedSetKey { get; set; }
public int KeyOrder { get; set; }
public string Value { get; set; }
}
Using .net core 5, npgsql
I am following this tutorial, using Visual Studio Community 2015 v 14.0.25431.01 Update 3 and MS .NET Framework v 4.6.91586, but get the following error when I attempt to scaffold the controller, as described:
I have tried all the suggested solutions here and here, but to no avail. Yes, also tried (re)building the project.
Here is the relevant code from my project.
Student Model:
using System;
using System.Collections.Generic;
namespace ContosoUniversity.Models
{
public class Student
{
public int ID { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public DateTime EnrollmentDate { get; set; }
public ICollection<Enrollment> Enrollments { get; set; }
}
}
SchoolContext:
public class SchoolContext : DbContext
{
public SchoolContext(DbContextOptions<SchoolContext> options) : base(options)
{
}
public DbSet<Course> Courses { get; set; }
public DbSet<Enrollment> Enrollments { get; set; }
public DbSet<Student> Students { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Course>().ToTable("Course");
modelBuilder.Entity<Enrollment>().ToTable("Enrollment");
modelBuilder.Entity<Student>().ToTable("Student");
}
}
Added the SchoolContext to the services in startup.cs:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddDbContext<SchoolContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
And this is my connectionstring in appSetting.json:
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-ContosoUniversity-75b88406-9d10-4237-814a-3f725b9b4e3d;Trusted_Connection=True;MultipleActiveResultSets=true"
}
Okay, so I've managed to get over the problem of not having a parameterless constructor. As suggested here, I simply provided the constructors to both the DBContext class as well as the Model class as such:
public Student() { }
Now I'm getting a different error. I guess I need to pose it as a new question. I will, but for what it's worth, this is part of the error log:
Attempting to figure out the EntityFramework metadata for the model and DbContext: Student
Exception has been thrown by the target of an invocation. StackTrace:
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.AspNetCore.Hosting.Internal.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
at Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore.EntityFrameworkServices.TryCreateContextUsingAppCode(Type dbContextType, ModelType startupType)
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 52 - Unable to locate a Local Database Runtime installation. Verify that SQL Server Express is properly installed and that the Local Database Runtime feature is enabled.) StackTrace:
...
System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
There is no entity type Student on DbContext ContosoUniversity.Data.SchoolContext at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject,...
I was just trying some features of entity framework 5 and kendo ui. I have a following ProductType enum
public enum ProductType {
[Description("Hazardous")]
Hazardous,
[Description("Non Hazardous")]
NonHazardous
}
This enum type is one of the field in the Product Entity.
[Table("Products")]
public class Product {
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
//Other Fields ...
[Required(ErrorMessage="Product Type is Required")]
public ProductType ProductType {get;set;}
}
The ProductType column in the underlying SQL Server Database is defined as (tinyint, not null).
I access list of Products in the following Controller Action of MVC
public ActionResult _ProductList(int pageSize, int skip) {
using (WorkUnit workUnit = new WorkUnit()) {
IQueryable<Product> products = workUnit.ProductRepository.GetAllProducts()
.Include(p => p.Category);
int total = products.Count();
List<Product> productList = products.ToList<Product>(); //Throws InvalidOperationException
return Json(new { total = total, data = productList }, JsonRequestBehavior.AllowGet);
}
}
Here is the description of the InvalidOperationException that is thrown on products.ToList() in the above controller action.
The 'ProductType' property on 'Product' could not be set to a 'Byte' value.
You must set this property to a non-null value of type 'ProductType'.
Any guesses why ToList() is throwing an InvalidOperationException.
Just for info, the Database existed previously, and the Entities were manually coded based on the Database as POCO's.
Edit
Here is the complete exception details
System.InvalidOperationException was unhandled by user code
HResult=-2146233079
Message=The 'ProductType' property on 'Product' could not be set to a 'Byte' value. You must set this property to a non-null value of type 'ProductType'.
Source=System.Data.Entity
StackTrace:
at System.Data.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal)
at System.Data.Common.Internal.Materialization.Shaper.GetPropertyValueWithErrorHandling[TProperty](Int32 ordinal, String propertyName, String typeName)
at lambda_method(Closure , Shaper )
at System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
at lambda_method(Closure , Shaper )
at System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
at System.Data.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at KendoUI_Web_Demo.Controllers.HomeController._ProductList(Int32 pageSize, Int32 skip) in d:\Practice\ASP.NET_MVC\KendoUI_Web_Demo\KendoUI_Web_Demo\Controllers\HomeController.cs:line 52
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
InnerException:
There is a mismatch between the enum type which in your case is int based and your database where the column is byte (tinyint) based. You can fix this by setting the correct underlying type of your enum type:
public enum ProductType : byte
{
...
}
I've just switched our project to try Entity Framework 5.0 RC, running under .NET 4.0. Aside from updating namespace references, we haven't made any code changes. I'm getting the following the first time we try to access an entity framework object:
at System.Data.Entity.ModelConfiguration.Conventions.ForeignKeyNavigationPropertyAttributeConvention.System.Data.Entity.ModelConfiguration.Conventions.IEdmConvention<System.Data.Entity.Edm.EdmNavigationProperty>.Apply(EdmNavigationProperty navigationProperty, EdmModel model)
at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.Dispatch[TEdmDataModelItem](TEdmDataModelItem item)
at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.VisitEdmNavigationProperty(EdmNavigationProperty item)
at System.Data.Entity.Edm.Internal.DataModelItemVisitor.VisitCollection[T](IEnumerable`1 collection, Action`1 visitMethod)
at System.Data.Entity.Edm.Internal.EdmModelVisitor.VisitDeclaredNavigationProperties(EdmEntityType entityType, IEnumerable`1 navigationProperties)
at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.VisitEdmEntityType(EdmEntityType item)
at System.Data.Entity.Edm.Internal.DataModelItemVisitor.VisitCollection[T](IEnumerable`1 collection, Action`1 visitMethod)
at System.Data.Entity.Edm.Internal.EdmModelVisitor.VisitEntityTypes(EdmNamespace edmNamespace, IEnumerable`1 entityTypes)
at System.Data.Entity.Edm.Internal.EdmModelVisitor.VisitEdmNamespace(EdmNamespace item)
at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.VisitEdmNamespace(EdmNamespace item)
at System.Data.Entity.Edm.Internal.DataModelItemVisitor.VisitCollection[T](IEnumerable`1 collection, Action`1 visitMethod)
at System.Data.Entity.Edm.Internal.EdmModelVisitor.VisitNamespaces(EdmModel model, IEnumerable`1 namespaces)
at System.Data.Entity.Edm.Internal.EdmModelVisitor.VisitEdmModel(EdmModel item)
at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.VisitEdmModel(EdmModel item)
at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.Dispatch()
at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ApplyModel(EdmModel model)
at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source, Expression`1 predicate)
at BackEnd.Facade.Implementations.UserService.GetUserByUserName(String userName) in c:\Development\FundApps\Rapptr\src\FundApps.Rapptr.ServiceLayer\Facade\Implementations\UserService.cs:line 61
at FundApps.Rapptr.Common.Web.Security.WebUserContextModule.OnPostAuthenticateRequest(HttpContextBase context) in c:\Development\FundApps\Rapptr\src\FundApps.Rapptr.Common.Web\Security\WebUserContextModule.cs:line 54
at FundApps.Common.Mvc.BaseHttpModule.<Init>b__1(Object sender, EventArgs e) in c:\Development\FundApps\Rapptr\src\FundApps.Common.Mvc\BaseHttpModule.cs:line 14
at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Does anyone have any suggestions as to what might cause this and how to narrow it down?
UPDATE: This bug was fixed in EF5 RTM.
This is a bug in EF5. It happens when you have a 1:1 relationship and also use the ForeignKey attribute to explicitly specify the FK. For example:
public class OneToOneResult
{
public int OneToOneResultId { get; set; }
[ForeignKey("OneToOneResultId")]
public virtual OneToOneResultDetail Detail { get; set; }
}
public class OneToOneResultDetail
{
[Key]
public int OneToOneResultId { get; set; }
public DateTime DataDate { get; set; }
}
In OnModelCreating:
modelBuilder.Entity<OneToOneResult>()
.HasRequired(r => r.Detail)
.WithRequiredPrincipal();
The workaround is to remove the ForeignKey attribute. It is not needed in this case because the 1:1 relationship must be PK to PK since that is all that EF supports in this case.