ef-code-first

Best practice for Ids Entity framework code first

半世苍凉 提交于 2019-12-24 07:39:33
问题 So I stumbled upon this article earlier today https://blogs.msdn.microsoft.com/azuremobile/2014/05/22/tables-with-integer-keys-and-the-net-backend/ In the article, the author makes a comment that got my attention. He said Note: Typically, when starting from an Entity Framework Code-First model in your .NET Backend, you would use string ids From what I've read, using string Ids can be a performance issue in as your table grows. So I would just like to know if this was just the authors opinion

Using custom methods in linq to entities

自作多情 提交于 2019-12-24 05:19:27
问题 I have a Person table in my database that has NationalId field. Is there any way to load all Persons with even NationalId , using Ef code first and Linq to entities , without loading all Person s to memory? Somethings like: public bool IsEven(int number) { return number % 2 == 0; } var context = new MyContext(); var personsWithEvenNationalId = context.Persons .Where(x=> IsEven(x.NationalId)) .ToList(); 回答1: You would have to do your check inline var personsWithEvenNationalId = context.Persons

Entity Framework: Filtering Navigation Properties

本秂侑毒 提交于 2019-12-24 05:04:41
问题 I am working with Entity Framework - Code First, and i have the next context (an extraction at least): public class TestContext : DbContext { public DbSet<User> Users { get; set} public DbSet<Book> Books { get; set} } And in the user's class i have this navigation property: public virtual Collection<Book> Books { get; set} ergo, a user has many books. The problem is that i want to filter the books, but as i have like 500.000 books on my database, i can't afford bringing all the books to

How can I implement Query Interception in a LINQ to Entities query? (c#)

北城以北 提交于 2019-12-24 04:53:54
问题 I'm trying to implement encrypted columns in EF4 and using the CTP5 features to allow simple use of POCO's to query the database. Sorry that this is a lot of words, but I hope the below gives enough to explain the need and the problem! So, bit of background, and my progress so far: The intention is that if you query the tables without using our DAL then the data is rubbish, but I don't want the developers to worry about if/when/how the data is encrypted. For simplicity, at this stage I'm

Entity Framework - bidirectional one-to-one relationship on a foreign key

百般思念 提交于 2019-12-24 04:44:07
问题 I have two entities like Hat and Owner: +========+ +=========+ |Owner |----------|Hat | +--------| 0/1 1 +---------| |ID | |ID | |Name | |Size | +--------| +---------| |HatId | |OwnerId | +========+ +=========+ (Every Owner has his Hat. Some Hats does not have their owners.) I've created models: public class Owner { [Key] public Int32 ID { get; set; } public String Name { get; set; } public virtual Hat Hat { get; set; } } public class Hat { [Key] public Int32 ID { get; set; } public Int32

Entity Framework code first - Many to Many - Include conditional

杀马特。学长 韩版系。学妹 提交于 2019-12-24 04:08:05
问题 I have two Entities Store and Catalog , having many to many relationship using fluent Api. I want to get a Store by id with all the catalogs having status equals to "Published". Below I try to write the following query but not getting the expected results. var store = context.Stores.Include("Catalogs").Where(s => s.StoreID == id && s.Catalogs.Any(c => c.Status == "Published")).SingleOrDefault(); 回答1: What you're asking for there is "give me the store with this ID, but only if it has a

Lost migrations and Azure database is now out of sync

流过昼夜 提交于 2019-12-24 04:02:15
问题 The quick question is "How do I get my Azure database back in sync with my local database?" Details: I have been using entity framework in my project, which really not that different from the ASP.NET MVC 5 sample project, which has Identity 2 built in. There are two database contexts, one called ApplicationDbContext (aspnet_somethingUserDB) from Identity and the other one called StoreInitialTestContext (store_db) that is from my changes. Interestingly, I have each of these contexts in a

Saving Entity causes duplicate insert into lookup data

烈酒焚心 提交于 2019-12-24 03:32:16
问题 I am using EF 4.1 "code first" to create my db and objects. Given: public class Order { public int Id { get; set; } public string Name { get; set; } public virtual OrderType OrderType { get; set; } } public class OrderType { public int Id { get; set; } public string Name { get; set; } } An order has one ordertype. An order type is just a look up table. The values dont change. Using Fluent API: //Order ToTable("order"); HasKey(key => key.Id); Property(item => item.Id).HasColumnName("order_id")

Entity Framework Code First Circular dependencies

回眸只為那壹抹淺笑 提交于 2019-12-24 02:23:37
问题 I am a bit confused with Circular dependencies with EF as its seems like everything will just become a circular dependency. Looking at this tutorial public class Blog { public int BlogId { get; set; } public string Name { get; set; } public virtual List<Post> Posts { get; set; } } public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } public int BlogId { get; set; } public virtual Blog Blog { get; set; } } When I run the "code

Why aren't my foreign key reference properties reflecting the foreign key ID property values?

早过忘川 提交于 2019-12-24 00:59:55
问题 I have a Candidate entity, with some associations exampled below. I have a view model that doesn't have the reference properties, just the foreign key properties, and I am using AutoMapper to map the view model back to an entity. When I try and save the entity, I get validation errors telling me that e.g. The Title field is required. because although my TitleId has a valid value, Title is still null. public class Candidate { ... [Required] public string RefNum { get; set; } [ForeignKey(