ef-code-first

System.Data.DataException: An exception occured while initializing the database

ぃ、小莉子 提交于 2019-12-14 04:20:08
问题 Please help me on this. I have written a code for getting the first record from the database using LINQ My Homecontroler [CustomHandleError] public class HomeController : Controller { Domain domain; public HomeController() { domain = new Domain(); } public ActionResult Index() { HomePageViewModel vm = new HomePageViewModel(); vm.Page = domain.GetHomePageInfo(); if (vm.Page != null) { ViewBag.Title = vm.Page.PageTitle; ViewBag.Keywords = vm.Page.PageKeywords; ViewBag.Description = vm.Page

How to implement a Quartz.NET job in ASP.NET MVC with UnitOfWorkPattern on EF Code first

删除回忆录丶 提交于 2019-12-14 03:51:03
问题 I have ASP.NET MVC 3.0 application, with EF Code First for data layer. I have implemented a Unit of work pattern, I’m binding context of the unit of work on HttpContext.Current.Items[SomeKey] collection. Unit of work is created and committed in OnActionExecuting/Executed events on controller. I’m instantiating repositories using Windsor Castle. Now I need to use Quartz.net to run a job periodically in my app, this job need also use few repositories. The problem is, that in a SchedulerJob

Entity Framework Many to Many works but Include does not

五迷三道 提交于 2019-12-14 03:47:37
问题 I have a typical many-to-many relationship with these 3 tables [Post] ( [PostId] int, (PK) [Content] nvarchar(max) ... ) [Tag] ( [TagId] int, (PK) [Name] nvarchar ... ) [TagPost] ( [TagId] int, (PK, FK) [PostId] int (PK, FK) ) And, TagId and PostId are the PK and FK set on the tables accordingly etc. Then I have these classes and mapping in c# public class Post { public Post() { this.Tags = new HashSet<Tag>(); } [Key] public int PostId { get; set; } ... public virtual ICollection<Tag> Tags {

Run Code First Migration Seed Method without a migration

放肆的年华 提交于 2019-12-14 03:39:58
问题 How do you run a code first entity framework migration without changing anything in the datamodel which would cause a migration to be created? I just want to run the seed method again because I added things to it. 回答1: If you just need to run Seed() again, and nothing has changed that would cause a new migration to be added, just call Update-Database again with no flags and it will say 'No pending migrations', and run Seed() again for you. Seed() is called every time the DB is updated,

EF CodeFirst computed field in CF entity class

天涯浪子 提交于 2019-12-14 02:29:16
问题 I have added computed fields( Active and CreditsLeft ) directly into my CodeFirst entity class. Is it good idea to add computed field logic inside CF Entity class? public class User : Entity { public User() { Id = Helper.GetRandomInt(9); DateStamp = DateTime.UtcNow; TimeZone = TimeZoneInfo.Utc.Id; } [DatabaseGenerated(DatabaseGeneratedOption.None)] public int Id { get; set; } [Required] [MaxLength(50)] public string Email { get; set; } [Required] [MaxLength(50)] public string Password { get;

Customising Type Mappings in Entity Framework

隐身守侯 提交于 2019-12-14 02:18:03
问题 I'm using EF5 Code First and I'm trying to store an IPAddress object. If you try and do this directly, EF stores it as two columns, FIELDNAME_Address and FIELDNAME_Scope. Unfortunately, this is insufficient for storing IPv4 addresses, as accessing ScopeId throws an exception (see C# The attempted operation is not supported for the type of object referenced). What I'd like to be able to do is define how a type gets mapped in EF, but I'm not sure how to do that, or what the terminology for it

MYSQ & MVC3 SQL connection error \ ProviderManifestToken but I am using MySQL

て烟熏妆下的殇ゞ 提交于 2019-12-14 02:06:44
问题 This is my first time attempting full usage of mysql in .NET with MVC3, using code first technology. I have installed VS2010 (10.0.3), and MVC3, .NET 4 via web platform. I then installed MySQL .NET connector 6.4.3 . I then proceeded to setup a Data Connection in Server Explorer, and that seems to work fine. I configured the site using MySQL Website Configuration, and you can see the config settings below in the web.config. Then using Scott Hanselman's vid tutorial (http://www.asp.net/mvc

Code First Circular Reference Foreign Key Configuration

旧时模样 提交于 2019-12-14 02:01:23
问题 The following code creates foreign key errors when all code is not commented. public class Parent { public int Id { get; set; } public string Name { get; set; } public int FavoriteChildId { get; set; } public Child FavoriteChild { get; set; } //public int WorstChildId { get; set; } public Child WorstChild { get; set; } public ICollection<Child> Children { get; set; } } public class Child { public int Id { get; set; } public string Name { get; set; } //public int ParentId { get; set; } public

Specifying HasForeignKey with ModelBuilder?

早过忘川 提交于 2019-12-13 21:25:09
问题 Say I have a "Relationship" entity: public class Relationship { [Key] [Required] public int RelationshipId { get; set; } [Required] public int FriendOneId { get; set; } public virtual User FriendOne{ get; set; } [Required] public int FriendTwoId { get; set; } public virtual User FriendTwo { get; set; } } If I want to map these relationships with ModelBuilder, what is the difference between this: modelBuilder.Entity<Relationship>() .HasRequired(c => c.FriendOne) .WithMany() .HasForeignKey(u =>

EF6 Code First: Login Failing on update-database

邮差的信 提交于 2019-12-13 19:40:49
问题 I am trying to do an update-database on an EF6 code first database. All I've done is build my models and the datacontext, run enable-migrations and did an add-migration InitialCreate. That all worked well. But the update-database is failing in two different ways. For a while I had it working, but it was creating the database files (i.e., the MDF and log files) under c:\Users\. I don't want them there. I want them in the App_Data folder of an MVC website I'm building (the database schema is in