entity-framework

Why isn't server side validation working in my ASP.Net MVC3 application with Entity Framework?

不想你离开。 提交于 2020-01-05 07:56:12
问题 I have an ASP.NET MVC3 application that uses entities generated from a database. Each entity has also has a separate partial class that uses the MetadataType attribute to associate each entity with a class that is decorated with a number of validation attributes (see below). [MetadataType(typeof(Drawing.Metadata))] public partial class Drawing { private sealed class Metadata { [Required] [StringLength(50, MinimumLength = 3, ErrorMessage = "Drawing numbers must be between {2} and {1}

Adding properties to IdentityUser doesn't create the correct migration

流过昼夜 提交于 2020-01-05 07:45:21
问题 I've followed the tutorial here: Customizing profile information in ASP.NET Identity in VS 2013 templates And did exactly what's written there, except for that my new property is a string named 'TaxId'. Surprisingly, when I did the "Add-Migration" part, the migration file came up empty: namespace Mvc5Test.Migrations { using System; using System.Data.Entity.Migrations; public partial class TaxId : DbMigration { public override void Up() { } public override void Down() { } } } Any idea why did

Programmatically creating a connection string for mapping an Entity Framework Code-First model with an existing Sql Server Compact database

北城余情 提交于 2020-01-05 07:38:36
问题 I've successfully mapped an Entity Framework Code-First data model with an existing Sql Server Compact database by a declarative approach using app.config but it would be great if I could build such connection programmatically with perhaps the help of the EntityConnectionStringBuilder (System.Data.EntityClient) class. Unfortunately this approach is not working as the [DbContext].Connection.ConnectionString is not accepting most of its properties. Here's the actual working code with the faulty

Code first database not being created from model

半腔热情 提交于 2020-01-05 07:37:36
问题 I'm creating a basic MVC application using asp.net and the entity framework. I've been following this tutorial, altering the data model to fit my needs. I've created a database model, and generated the MVC controller. When I run the application and navigate to a page that requires the database I get the following error when the line return View(db.Servers.ToList()); within the controller is reached: "An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll.

Caching EF domain objects: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection

不想你离开。 提交于 2020-01-05 07:32:29
问题 I know this exception is caused by some properties being defined as virtual and therefore it's trying to load them lazily after the context has been disposed. However, I don't want to load the entire object graph just to perform a fairly simple query. And if I try to cache the objects when the context is still alive, it tries to lazily load everything, and slows it down considerably . Is there any way of performing the query, and then getting a disconnected result set, so that it doesn't try

Can I use Data Annotations or Fluent to convert a value between data store and entity class?

心不动则不痛 提交于 2020-01-05 07:28:40
问题 I am working with an existing [poorly designed] database which stores a dollar amount as an integer (ie. $10.99 == 1099) in the database. I want my entity class to treat this value as a decimal so that consuming code doesn't have to do the translation. Is there a way to convert the value to and from integer to decimal and then back using Data Annotations or Fluent configuration? In the meantime I am using a hack which essentially "wraps" the integer property with a decimal property that

Edit Id - Store update, insert, or delete statement affected an unexpected number of rows (0)

左心房为你撑大大i 提交于 2020-01-05 07:21:26
问题 So I need to be able to edit my primary key field in my edit page for a row, I get the error Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries. When I don't change the primary key field I can save all my other changes to my database. This is my edit function. //edit - post [HttpPost] public ActionResult ActiveCampaignsEdit(ActiveCampaign activecampaign)

Adding code to Entity Framework 4 generated POCOs

江枫思渺然 提交于 2020-01-05 07:07:28
问题 Starting from an EF 4 entity diagram and using T4 templates one can create POCO classes that can be used inside the Domain Model. The generated code looks like this: public partial class Product { public virtual int Id { get; set; } public virtual string Name { get; set; } //and so on } Is there any elegant approach to add my own code for implementing the properties? for example, the Name setter I would like to be implemented by lowering all the characters. I would like that my code resist to

Can I easily evaluate many IQueryables in a single database call using Entity Framework?

喜欢而已 提交于 2020-01-05 07:04:40
问题 Suppose I have a collection (of arbitrary size) of IQueryable<MyEntity> (all for the same MyEntity type). Each individual query has successfully been dynamically built to encapsulate various pieces of business logic into a form that can be evaluated in a single database trip. Is there any way I can now have all these IQueryable s executed in a single round-trip to the database? For example (simplified; my actual queries are more complex!), if I had ObjectContext context = ...; var myQueries =

EF5 Code First TransactionScope without escalating to MSDTC

时光怂恿深爱的人放手 提交于 2020-01-05 06:52:51
问题 I'm using EF5 code first, and I need to wrap multiple calls to SaveChanges() in the same transaction. I'm relatively new to using transactionScope, and can't seem to tweak the options on it to accomplish what I'm looking to do. The following code resides in a service class that gets injected with an instance of my DbContext class. I do not create additional instances of DbContext anywhere in this class. At first I tried the following. The code is nice and clean, but it attempts to escalate to