entity-framework

MVC EF code first creating model class

ⅰ亾dé卋堺 提交于 2020-01-07 06:36:31
问题 I'm new to MVC and EF code first. I'm in struggle to model a real-estate company DB model using EF code-first approach and I did some exercises as well as reading some online tutorials. First thing I have a customers table that would be in relation with one or more properties he/she has registered as it's owner to sell or to rent, I was wondering if it is possible to have some sub classes inside a model class for registered properties as below: public Property { public int PropertyID { get;

remove a record with child [duplicate]

久未见 提交于 2020-01-07 06:36:13
问题 This question already has answers here : LINQ to Entities does not recognize the method (4 answers) Closed 2 years ago . //Delete Last Record which has income var itemToRemove = db.People.LastOrDefault(p => p.Incomes.Any()); db.Incomes.RemoveRange(itemToRemove.Incomes); db.People.Remove(itemToRemove); it gives me this error An unhandled exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll Additional information: LINQ to Entities does not recognize the

remove a record with child [duplicate]

杀马特。学长 韩版系。学妹 提交于 2020-01-07 06:36:07
问题 This question already has answers here : LINQ to Entities does not recognize the method (4 answers) Closed 2 years ago . //Delete Last Record which has income var itemToRemove = db.People.LastOrDefault(p => p.Incomes.Any()); db.Incomes.RemoveRange(itemToRemove.Incomes); db.People.Remove(itemToRemove); it gives me this error An unhandled exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll Additional information: LINQ to Entities does not recognize the

Dynamic Where in linq MVC

我与影子孤独终老i 提交于 2020-01-07 05:41:08
问题 I am trying to filter a model with two dropdown in an MVC project var model = (from x in db.TABLE.... join y in db.TABLE...).Where(where)... my logic is String where = string.Empty; if (search.anno != null) where = " ANNO = " + search.anno ; if (search.Cliente != null) { if (!string.IsNullOrEmpty(where)) { where += " And CODICE_CLIENTE = '" + search.Cliente + "'"; } else { where = " CODICE_CLIENTE = '" + search.Cliente + "'"; } } i get an error: System.Linq.Dynamic.ParseException: Character

Entity Framework, relation between two tables

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-07 05:35:09
问题 My model classes are: public class User { public User() { Polls = new List<Poll>(); } public int Id { get; set; } public String FirstName { get; set; } public String LastName { get; set; } ICollection<Poll> Polls { get; set; } } public class Poll { public int Id { get; set; } public int PositiveCount { get; set; } public int NegativeCount { get; set; } public String Description { get; set; } public User User { get; set; } } public class PollsContext : DbContext { public DbSet<User> Users {

How to query for all EntityA that contain any EntityB within a collection of EntityBs?

和自甴很熟 提交于 2020-01-07 05:26:06
问题 Given the following two example C# entity classes ... class EntityA { public int EntityAId { get; set; } public virtaul ICollection<EntityB> Bs { get; set; } } class EntityB { public int EntityBId { get; set; } public string Foobar { get; set; } public virtual EntityA A { get; set; } } How would I go about using Entity Framework + LINQ to query for all EntityA that contain any EntityB within a provided collection of EntityB ? To clarify, if I have the following subset of EntityB ... var bs =

Why is this Exception?- The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects

冷暖自知 提交于 2020-01-07 05:23:07
问题 I m getting this Exception-"The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects." I ve user table and country table. The countryid is referred in user table. I am getting the above Exception when I am trying to add entry in user table. This is my code- using (MyContext _db = new MyContext ()) { User user = User .CreateUser(0, Name, address, city, 0, 0, email, zip); Country country = _db.Country.Where("it.Id=@Id", new

How do I get a property's original value while validating, in EF Core?

纵饮孤独 提交于 2020-01-07 05:16:06
问题 In EF Core, there is a neat way to perform entity validation using IValidatableObject . During validation I have the current value, but I also need access to the original value. This is the validation method: public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) { } Is there some way to get access to the object's original values as well? I think the key is in ValidationContext but I'm not sure how. 来源: https://stackoverflow.com/questions/41771376/how-do-i-get-a

Entity Framework Auto Migrations Primary Key

♀尐吖头ヾ 提交于 2020-01-07 04:55:13
问题 I have inherited a sql server database and an ASP.Net MVC 4 web application which is using Entity Framework 5.0 Code First with Auto Migrations . However, it appears the previous developer forgot to add a Primary Key to one of the tables. I am now trying to do this using Auto Migrations , however, it is not working, no errors either, just seems to be ignoring the command. The table is like this public int CourseDateHistoryID { get; set; } public int CourseDateID { get; set; } public int Event

Filter data with multiple complex clauses joined with OR operation in where function

眉间皱痕 提交于 2020-01-07 04:50:19
问题 How is it possible to filter data based on some set of filters or expressions that should be used with or operation in where clause? For example, there is a class: class DTOFilter { public string Domain { get; set; } public string Mobile { get; set; } } It is required to filter Users list based on the list of the filters next way: u=> (u.Email.Contains(filters[0].Domain) && u.PhoneNumber.StartsWith(filters[0].Mobile)) || (u.Email.Contains(filters[1].Domain) && u.PhoneNumber.StartsWith(filters