ef-code-first

Attach entity in modified state without marking all properties dirty

允我心安 提交于 2019-12-12 10:34:02
问题 I'm trying to figure out how to mark specific properties of a detached entity as modified. If I do the following, it will mark all properties modified and the generated sql will update all columns. /// <summary> /// Sets the entity in the modified state. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity">The entity.</param> void IDbContext.Modified<T>(T entity) { DbEntityEntry<T> entry = Entry(entity); if (entry.State == EntityState.Modified) { // if the state is

Define SQL table primary key as case sensitive using Entity Framework Code First

丶灬走出姿态 提交于 2019-12-12 09:57:45
问题 Is it possible to define a SQL case sensitive primary key in entity framework code first? I know that by default SQL is case insensitive when it comes to strings, just to be clear I don't want to change the entire DB definition to case sensitive, just one table column (primary key). I'm getting data from an external API which sends the data with case sensitive primary keys ('a' and 'A' are different records), I know I can modify them and save them differently in my DB, but this will also

How do I represent an option calculated column with EF Code First?

天大地大妈咪最大 提交于 2019-12-12 09:52:54
问题 I have a situation where I have a table of entries that I'll be querying on, but in some cases I'll have additional information available. For instance, if I have a table of people , I want to be able to search by name, but I also want to store some coordinates and search based on their location, but also expose that distance in the object model. So, for instance, suppose my people table looks something like this: PersonId int Name nvarchar(100) Address nvarchar(100) Latitude float(10,6)

EF Code First adds extra column to query that doesn't exist in model anymore

。_饼干妹妹 提交于 2019-12-12 09:51:22
问题 This is my first question on SO. I have an ASP.NET 4.0 MVC3 project that is using EF Code First as ORM and FluentMigrator for version migrations. And there I have Message entity class that looks like this: public class Message { [Key] [Column("Message_Id")] public int Id { get; set; } public DateTime CreatedTime { get; set; } [Required] [StringLength(MaxSubjectLength)] public string Subject { get; set; } [Required] [StringLength(MaxBodyLength)] public string Body { get; set; } public Link

Set a constraint for minimum int value

人走茶凉 提交于 2019-12-12 09:44:56
问题 I am using the Repository pattern. I have a entity called Product and I want to set the minimum value for price to avoid zero prices. Is it possible to create it in a EntitytypeConfiguration class? My product configuration class public class ProductConfiguration : EntityTypeConfiguration<Product> { public PlanProductConfiguration(string schema = "dbo") { ToTable(schema + ".Product"); HasKey(x => new { x.IdProduct }); Property(x => x.Price).HasColumnName("flt_price") .IsRequired()

Execute Code First Migrations on Azure with Remote Connection String

爷,独闯天下 提交于 2019-12-12 09:18:08
问题 I am publishing an Azure Web App, and I'm running into a strange issue executing code first migrations. The remote connection string for this database is working just fine if I do not check "Execute Code First Migrations". However, if I check the box, a strange connection string with a curious misspelling, "ConnetionString", gets injected into the remote web.config: <add name="DbContext_DatabasePublish" connectionString="DbContext_DatabasePublish.ConnetionString" providerName="System.Data

Implement WCF Data Service using the Repository Pattern

六眼飞鱼酱① 提交于 2019-12-12 08:57:20
问题 We are using the repository pattern in our ASP.NET MVC 3 application. This means that, although we use EF 4.1 Code First to access the data in the backend, all MVC controllers do that via a generic repository class rather than directly over the DbContext subclass. Simplified code snippet: public class MyEntityContext : DbContext, IMyEntityContext { public IDbSet MyEntities { get; set; } ... } public class MyEntityRepository : IMyEntityRepository { private IMyEntityContext _context; public

String or binary data would be truncated.The statement has been terminated

穿精又带淫゛_ 提交于 2019-12-12 08:48:06
问题 I am updating my database using the update-database command in Entity Framework and it is raising an error with the message: String or binary data would be truncated. The statement has been terminated How can I allow the update to work? 回答1: Take this code first entity: public class Something { public string SomeProperty { get; set; } } That will create a column of type NVARCHAR(MAX) which will let you pretty much store any size text. Now we apply an annotation like this: public class

Entity Framework 4.1 Code First - Define many-to-many using data annotations only

﹥>﹥吖頭↗ 提交于 2019-12-12 08:34:25
问题 Is it possible to define a many-to-many relationship in Entity Framework 4.1 (Code First approach) using Data Annotations only, without model builder? For example, something like: Product = { Id, Name, ... } Category = { Id, Name, ... } ProductCategory = { ProductId, CategoryId } You get the picture. I don't want to have an intermediate entity ProductCategory in the context with two many-to-ones since I don't have any additional data, just the two FKs. Also, I should be able to define table

Entity Framework - Seed AddOrUpdate with multi column index as identifier

让人想犯罪 __ 提交于 2019-12-12 08:28:52
问题 I am trying to seed a database using the context.AddOrUpdate method, but the problem is that I need to make the inserted data unique based on a multi column index. [Table("climbing_grades")] public class ClimbingGrade : EntityBase { /// <summary> /// The name of the climbing grade, e.g.: 7a, VII, etc. /// </summary> [Index("IX_Name_GradeType", 1, IsUnique = true)] public string Name { get; set; } /// <summary> /// Tries to display the average difficulty of the described grade. /// Matching