ef-code-first

Can I Embed an object in an EF entity (serialize on save, deserialize on access)?

此生再无相见时 提交于 2019-12-17 22:15:37
问题 I have a class that I want to keep meta data for -- there a several interaction scenarios so meta allows me to keep different meta for different interaction types. class Feed() { Guid FeedId { get; set; } ObjectMetaDictionary Meta { get; set; } } I would like EF to serialize this ObjectMetaDictionary and store it as a string/VarChar in the database. When I retrieve a record I want it to be deserialized as an ObjectMetaDictionary. Does EF support this? How can I do it? I am using Entity

migration synch developmental and production databases

只愿长相守 提交于 2019-12-17 21:23:49
问题 I am using MVC 5 with NET Framework 4.5.1. with Code-first. I am also using Migrations with the SQL 2012 server and (localdb)\v11.0. I am in the middle of developing a project using C# and MVC5. During development, I created a lot of new tables in my developmental computer and changed a the "Name" field which I believe the system makes an index for. I added it and deleted it several times. After that, I added a lot iof new unrelated tables, but for some reason, my migrations started giving me

Why does IsInRole always return false?

為{幸葍}努か 提交于 2019-12-17 20:43:19
问题 I am building an MVC5 application with ASP.NET Identity 2.0 and EF 6.1.1. This is part of my current Login method: [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) { if (!ModelState.IsValid) return View(model); var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: true); switch (result) { case SignInStatus.Success: { var user = await UserManager

Inheritance and composite foreign keys - one part of the key in base class, the other part in derived class

可紊 提交于 2019-12-17 20:35:31
问题 I am having problems to create an Entity Framework Code-First mapping for the following sample database schema (in SQL Server): Every table contains a TenantId which is part of all (composite) primary and foreign keys (Multi-Tenancy). A Company is either a Customer or a Supplier and I try to model this via Table-Per-Type (TPT) inheritance mapping: public abstract class Company { public int TenantId { get; set; } public int CompanyId { get; set; } public int AddressId { get; set; } public

Entity Framework Code First - Change Table Column Collation

Deadly 提交于 2019-12-17 20:17:00
问题 I'm using Entity Framework CTP5 and Code First. I need to change the Collation for a specific column in SQL Server. I believe the default collation is SQL_Latin1_General_CP1_CI_AS, but I need to change this one column colllation to SQL_Latin1_General_CP1_CS_AS (Case Sensitive). Is there a way to use ModelBuilder in Code First to change a specific column collation? BarDev 回答1: Model builder doesn't allow this but you can create custom database initializer and execute ALTER TABLE command. The

Database.SetInitializer to null not working Entity Framework 4.3.1 Code First

孤街醉人 提交于 2019-12-17 19:52:36
问题 I have a context class that inherits from an abstract base AuditableDbContext : DbContext . The AuditableDbContext takes two parameters, one for the auditor and one for the context to audit into. In the inherited class, I have a default parameterless constructor that calls the other constructors with null parameters and then in the final constructor I call Database.SetInitializer<MyDbContext>(null) after calling the base constructor. The problem is that even when I do this, I still get the db

Entity Framework: field of composite key cannot be nullable?

天大地大妈咪最大 提交于 2019-12-17 19:33:25
问题 I have a model with composite key - the row is the key: public class Item { [Key, Column(Order = 0)] public int UserId { get; set; } [Key, Column(Order = 1)] public DateTime? Date { get; set; } } Running the code below it throws an exception DbEntityValidationException with message: The Date field is required. : var it = new Item { Date = null, UserId = 2 }; m_Entities.Items.Add(it); m_Entities.SaveChanges(); // throws exception ( m_Entities is usual DbContext descendant with Items defined as

How to limit number of related data with Include

对着背影说爱祢 提交于 2019-12-17 19:23:13
问题 class Cat { public int CatID; public string Name; public ICollection<Post> Posts; } class Post { public int PostID; public string Name public int CatID; public virtual Cat Parent; } And I want to load all the Catergories with their Posts so: var cats = context.Cat.Include(c => c.Posts); Now I want to limit the number of Posts that are returned, can someone show mw how to do that? I'm using EntityFramework 4.3.1 回答1: It is not possible with eager loading ( Include ) - eager loading returns

In what scenarios do I need foreign keys AND navigation properties in entity framework

不问归期 提交于 2019-12-17 19:16:24
问题 My Order class has: public int CustomerId { get; set; } public Customer Customer { get; set; } Do I really need both properties to make a relation working? I am not using disconnected entities, I am using code first approach. 回答1: According to Julia Lerman's book: Programming Entity Framework: DbContext, the difference lies at the difficulty of updating the navigation property. In page 85, She suggests "If there is one thing you can do to make your life easier in N-Tier scenarios, it’s to

How to configure DbContext to work with Oracle ODP.Net and EF CodeFirst?

爷,独闯天下 提交于 2019-12-17 18:58:37
问题 I'm trying to work with EF CodeFirst under Oracle with ODP.net. This is my DbContext class: public class MyCEContext : DbContext { public DbSet<Person> Persons { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Person>().ToTable("PERSONS","myce"); } public MyCEContext() : base(new OracleConnection( "Data Source=cebd; User ID=myce; Password=****;"), true) {} } Problem is that when I try to do something like this: MyCEContext context = new