entity-framework-5

Get ignored properties in Entity Framework

你。 提交于 2019-12-07 17:04:23
问题 I work on a framework with EF. I want to get all ignored properties of an entity to build some special queries. How can I do it? public class Customer { public int Id { get; set; } public DateTime BirthDate { get; set; } public int Age { get; set; } } public class CustomerContext : DbContext { protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Customer>().Ignore(customer => customer.Age); base.OnModelCreating(modelBuilder); } public DbSet<Customer>

One-to-one relationship in CF Entity Framework using mapping

℡╲_俬逩灬. 提交于 2019-12-07 14:42:10
问题 I'm using Entity Framework 5, Code-First. I've two domain objects (or tables). 1st is User , and 2nd is UserProfile . One user can have only one profile, and one profile belongs to only one user. That is 1-1 relationship. Here are the classes.... (I simplified the code to make it understandably, It is actually more complex) User public class User { public virtual Int64 UserId { get; set; } public virtual UserProfile UserProfile { get; set; } public virtual String Username{ get; set; } public

Entity Framework Many to Many query

↘锁芯ラ 提交于 2019-12-07 13:24:06
问题 I want to write a simple query, but there are some problems. I have 2 tables M to N: Users -> Events. I want to get all users of a specific event (get this event by eventId). public IQueryable<User> GetUsersByEventId(int eventId) { IQueryable<User> query = this.Context.Users.AsQueryable(); return query.Where(x => x.Events.SingleOrDefault(e => e.EventId == eventId)); ?? } Something is missing and I dont know what, can someone help me? Thanks a lot ! 回答1: If I understand you correctly (adding

How to specify a column Name for EF5 navigation property

旧巷老猫 提交于 2019-12-07 11:02:24
问题 I'm using EF5 code first to generate my database schema, but my new navigation property is being named in an undesirable way in the table. here is the model I'm working with. public class User { [Key] public long UserId { get; set; } ... **public virtual ICollection<ImagePermission> KeepThisNavigationName { get; set; }** } However, After I've updated my database and examine the table columns, the column is named: dbo.ImagePermission.User_UserId And I would like it to be named dbo

Log4net with Entity Framework 5 and MVC4

会有一股神秘感。 提交于 2019-12-07 09:53:15
问题 So I am starting this new project and want to use Log4net to log to the Database. I am using EF for other things and it connects fine. For some reason L4N is not logging anything but not throwing any errors. Just wondering what step(s) I may be missing, hope someone can point it out. Do I need to "save" to the db somehow? Thanx so much in advance. My web.config: <log4net> <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender"> <bufferSize value="100" /> <connectionType value=

Private backing field for entity framework lazy loading

久未见 提交于 2019-12-07 09:36:33
问题 I'm using Entity Framework 5 with lazy loading enabled. I have got the following code: private ICollection<Subscription> _subscriptions = new Collection<Subscription>(); public virtual ICollection<Subscription> Subscriptions { get { return _subscriptions; } set { _subscriptions = value; } } But does this make sense? I want to ensure that the public property Subscriptions is never null. Due to the virtual entity framework overrides the getter and setter to provide the lazy loading

Is there a better way to add a child record using MVC 4 and Entity Framework 5?

送分小仙女□ 提交于 2019-12-07 08:44:10
问题 I am just learning MVC and dealing with its stateless nature in combination with Entity Framework. My question is, is there a more elegant way of handling the scenario below? I have two POCO entities: public class Contest { public long ID { get; set; } .... ..... public ICollection<ContestPrize> Prizes { get; set; } } public class ContestPrize { public long ID { get; set; } public Contest Contest { get; set; } } I then have a MVC View that displays the Contest and all the Prizes associated

The value “(string)” is invalid

余生长醉 提交于 2019-12-07 07:23:46
问题 I currently have a view that contains two text boxes where users can enter in some data. One text box only allows values of 1-10, the other a string. I am not sure what code change I made, but the second text box accepting a string no longer "works". For example, when I enter in a string and try to submit the form, I get a validation message that states "The value "(string)" is invalid. Below are some code snippets in my solution. Entity: public class MovieReview { public int Id { get; set; }

Entity Framework Code First - How to ignore a column when saving

让人想犯罪 __ 提交于 2019-12-07 06:36:41
问题 I have a class called Client mapped to a database table using Entity Framework code first. The table has a computed field that I need available in my Client class, but I understand that it won't be possible to write to this field. Is there a way of configuring Entity Framework to ignore the property when saving, but include the property when reading? I have tried using the Ignore method in my configuration class, or using the [NotMapped] attribute, but these prevent the property from being

Using ToString() in LINQ queries?

大兔子大兔子 提交于 2019-12-07 06:13:48
问题 I have writting a LINQ query to fill a listview but it useses the .ToString() method which apparetly is not allowed. When I use the below code I get the error message: Error: LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression Is there a way to use the ToString() in LINQ or if that is not possible what is the solution to converting a DateTime to String in the query. Please knot that ReleaseDateName is a