code-first

Adding a migrations gives filename already exists

微笑、不失礼 提交于 2019-12-23 12:39:15
问题 I have enabled migrations as such: enable-Migrations -ProjectName ProjectOne -ContextTypeName MyIdentity.Config.MyIdentityContext -MigrationsDirectory Identity\\Migrations I specified my context as it is in a separated namespace, and i specified the directory because i want to have the migrations in a different directory. After enabling the migrations like this, i get the expected configuration file in the expected location (Identity\Migrations folder) (i removed the comments in the seed

How to use DBContext.Add/Attach (using EF CodeFirst 4.1) with nested opbjects

懵懂的女人 提交于 2019-12-23 12:18:24
问题 Problem: When adding an object "Order" to my dbcontext, all nested objects of the order gets "readded" to the database, though the nested objects is static data and only a reference shoudl be added in the database. Example: The database holds 0 orders, and 3 items. I add one order with 2 items. Now the database hold 1 order, and 5 items. The two items in the order has been "readded" to the database, even though the items had the right primary keys before db.SaveChanges(). I realize that i may

Entity Framework getting confused about navigation property

折月煮酒 提交于 2019-12-23 10:25:54
问题 I'm using Entity Framework 6.1.1 and I have a Users table and a User_Documents table (1:many). I already had a navigation property from User_Documents to User were things were working fine. public partial class User_Document { [Key] [DatabaseGenerated(DatabaseGeneratedOption.None)] public long User_Document_ID { get; set; } public long User_ID { get; set; } [ForeignKey("User_ID")] public virtual User User { get; set; } } I added a navigation property from Users to User_Documents public

[Win32Exception (0x80004005): The system cannot find the file specified]

早过忘川 提交于 2019-12-23 09:08:09
问题 I'm working on a joke web application. I work with C#, Entity Framework, MVC and I do code-first. Now when i try to read out something in my DB, the error I put in the title shows up. And I have no idea what to look for. I looked up Google but I didn't quite get an answer... I try to provide all the code that you could need to know: public class Context : DbContext { public DbSet<Categorie> Categories {get;set;} public Context() : base("Witze_DB") { } } public class HomeController :

Code first causing required relation to be optional?

ぐ巨炮叔叔 提交于 2019-12-23 09:01:05
问题 public class Client { public Int32 ClientID { get; set; } public virtual ICollection<Inquiry> InquiryManufacturers { get; set; } public virtual ICollection<Product> Products { get; set; } public virtual ICollection<Inquiry> InquiryRetailers { get; set; } } public class Product { public Int32 ProductID { get; set; } public Int32 ClientID { get; set; } public virtual Client Client { get; set; } public virtual ICollection<Inquiry> Inquiries { get; set; } } public class Inquiry { public Int32

EF Code First forced eager loading

谁说我不能喝 提交于 2019-12-23 07:49:18
问题 I'm using EF 5 with Code First. I have a class that I want to always eager load some properties. I removed the virtual keyword but it's not eager loading: public class Person { public ICollection<Email> Emails { get; set; } public Profile Profile {get;set;} } So by turning off lazy loading, it won't auto eager load right? If so, how do I archive that without using Include()? Thanks! 回答1: No, turning off lazy loading by removing the virtual keyword will not automatically enable eager loading.

Code First - UML - Modeling of database - visual view possible?

依然范特西╮ 提交于 2019-12-23 06:04:11
问题 I love how model first gives a visual overview of the database. But now the Ado.Net team pushes code first, i'd think it would be awesome to generate an UML overview of the database through your classes. Does this already exists? As i can't seem to find it :( 回答1: Yes it exists as part of EF Power Tools CTP1 where you can generate read-only EDMX from your code first mapping. It is not UML but it is the same diagram you had with model first. 回答2: UML is representing an object approach while

Updating DbSet<T> item's value from the controller - C#, MVC, Code First

二次信任 提交于 2019-12-23 03:51:59
问题 here's my question: I have an MVC3 C# application and I need to update a value in a DbSet from the Edit controller, so the T item's value will be replaced with a new one. I don't want to delete the item and add it again. I cannot figure out how to do this. DbSet doesn't seem to have something like an index. Here's my Edit controller: public class ItemController : Controller { private SESOContext db = new SESOContext(); private FindQrs fqr = new FindQrs(); [HttpPost] public ActionResult Edit

Entity Framework Code First Casting Columns

六月ゝ 毕业季﹏ 提交于 2019-12-23 03:25:40
问题 I have an class public class Foo { public int UserId { get; set; } } and I'm using the code first fluent mapping to map this to the database. Property(i => i.UserId) .HasColumnName("userno"); the only problem is that userno is actually a char(10) in the database. How do I go about casting or converting this type? as I currently get this error. The 'UserId' property on 'Foo' could not be set to a 'String' value. You must set this property to a non-null value of type 'Int32'. Thanks 回答1: Entity