entity-framework-6

Entity framework 6 Concept clear regarding Remove, RemoveRange, EntityState.Deleted

[亡魂溺海] 提交于 2019-12-25 04:14:05
问题 I use Entity framework 6 in my projects and I always have doubts regarding some of the concepts which are used to delete objects using EF. I still don't know which one works in which scenario. I just try all and if one works I leave it until the code is working. But no wi need to understand this concept once and for all. I did my research my unable to understand the concept clearly. I have a domain class in EF which have multiple referencing entities. For example. I have a domain class called

The data reader is incompatible with the specified class

a 夏天 提交于 2019-12-25 03:54:35
问题 so there are a few other similar type posts about, but none matching the simple task I am attempting. just running a stored proc through EF6 code first in the following manner var results = context.DataBase.SqlQuery<MyClass>("exec spGetStuff @param1, @param2", new SqlParameter[] {new SqlParameter("param1",value), new SqlParameter("param2", value2)}); I have used this method on many occasions with no issue. the class I am mapping the results to is pretty nasty with many properties, but all

Extend the Where method for IQueryable [duplicate]

萝らか妹 提交于 2019-12-25 03:22:51
问题 This question already has an answer here : extend Where for IQueryable (1 answer) Closed 5 years ago . I want to extend the Where to something like this: .WhereEqual("Field", "SomeString") wich must be equivalent to .Where(p => p.Field== "SomeString") , here is my code: public static IQueryable<T> WhereEqual<T>(this IQueryable<T> q, string Field, string Value) { var param = Expression.Parameter(typeof(T), "p"); var prop = Expression.Property(param, Field); var val = Expression.Constant(Value)

Switching to artificial key with different type in Entity Framework Migrations

半世苍凉 提交于 2019-12-25 02:59:48
问题 I'm working on an Entity Framework Code First project where, previously, I had a class with a field called "Id" that was a string type and using the hash. That's specified like this: [Key] public string Id { get; set; } That's no longer what I need because with updates I can now have duplicate values, so I want to change it to look like this: [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [Required] [Index] public string Hash { get; set; } //This represents

Entity Framework 6 Adding properties to join tables

泪湿孤枕 提交于 2019-12-25 02:53:07
问题 This is the scenario: I have a products table and a categories table. The relationship is many-to-many: a category can have 1 or more products....and a product can be in 1 or more categories... The Code-First mapping looks like this.... public class Product { //...additional properties... public virtual ICollection<Category> AssociatedCategories {get; set;} } public class Category { //...additional properties... public virtual ICollection<Product> AssociatedProducts {get; set;} } Now, under

Linq2Entities Equivalent Query for Parent/Child Relationship, With All Parents and Children, Filtering/Ordering Children

会有一股神秘感。 提交于 2019-12-25 02:22:18
问题 So the question is ridiculously long, so let's go to the code. What's the linq2entities equivalent of the following Sql, given entities (tables) that look like: Parent --- parent_id parent_field1 Child -- child_id parent_id child_field1 child_field2 The sql: select p.*, c.* from parent p inner join p on p.parent_id = child.parent_id where c.child_field1 = some_appropriate_value order by p.parent_field1 c.child_field2 L2E let's you do .include() and that seems like the appropriate place to

SaveChangesAsync() method not updating database

三世轮回 提交于 2019-12-25 02:15:41
问题 I have a web application with: Entity Framework 6.1.3 Framework .NET 4.5.1 I made an upgrade of .NET Framework from version 4.5.1 to 4.6.2 . Since that, the "SaveChangesAsync()" of "System.Data.Entity" method does not updating my database anymore. I can not find if it's because of the .NET framework package or some compatibility issues between Entity Framework 6.1.3 and .NET Framwork 4.6.2 for "SaveChangesAsync()" method... I'm a lost! Here's some samples: Service class public async Task

MySQL Entity Framework 6 Update affecting all rows Code First

梦想与她 提交于 2019-12-25 01:49:23
问题 I'm using Entity Framework 6 with MySQL. Inserting is fine, I was able to insert a new record. but when I tried to update the newly created record. It updates all the rows. Here is my code for saving. // This is the method I use every time I save a record. public async Task SaveAsync(User model) { if (model.Id != 0) { var original = GetOriginal(model.Id); if (model.Password == string.Empty) { model.Password = original.Password; } // The state is always modified if I'm updating the record. var

How do I edit a FormCollection value, and then use TryUpdateModel with the edited collection?

谁说我不能喝 提交于 2019-12-25 01:31:30
问题 MVC5 is storing my passwords in plaintext. I don't want to use the default hashing algorithm, as I'm required to use e.Encrypt() instead. I'm creating a registration function, and I need to know how I can edit values from FormCollection before using TryUpdateModel . Here's the code: [HttpPost] public ActionResult Register([Bind(Include = "User,Pass,Email")] FormCollection form) { var user = new Users(); string Hash = e.Encrypt(form["Pass"]); // Gets set. if (TryUpdateModel(user, form)) {

How to deploy with automatic migration enabled?

风流意气都作罢 提交于 2019-12-25 01:24:30
问题 I don't understand the concept of automatic migration. Having set AutomaticMigrationsEnabled = true; in the Migrations.Configuration class I can't find the place where migration steps are stored. How will Entity Framework recognize the current state of a production database and update it accordingly when, e.g., my console application is run at the customers' office? Any information on this is very appreciated. 回答1: To answer your first question: They aren't stored anywhere. Automatic