code-first

Code First migrations updating the wrong Db on localdb

旧巷老猫 提交于 2019-12-10 11:17:30
问题 I am using code first migrations to create and update the database for my asp mvc site. I have a DbContext, which is in another project in the solution. public class EFDbContext : DbContext { public DbSet<Book> Books { get; set; } public DbSet<Author> Authors { get; set; } public DbSet<Publisher> Publishers { get; set; } } When i enable-migrations and add-migration [name] . It seems to create its own db called [foo-projectname].Domain.Concrete.EFDbContext Instead of the attaching to the

Entity Framework 4 CTP 4 Code First: how to work with unconventional primary and foreign key names

孤人 提交于 2019-12-10 10:56:25
问题 Is there a way in Entity Framework 4 (using CTP4 and Code First if that matters) to change the conventions used to automatically identify primary and foreign keys? I'm trying to use EF with a legacy database that uses a "pk/fk" prefix rather than an "id" suffix to mark keys. Also, it's not uncommon to have multiple foreign keys to an Address table, for example, called "fkAddressHome" and "fkAddressWork". I would prefer a method for changing this behavior in a systematic way so that I don't

Multiplicity constraint violated SQL Server 2008 - CodeFirst

梦想的初衷 提交于 2019-12-10 03:08:59
问题 I'm working to solve a very tedious problem. I have a class called Nation and a class called NationAlly public class Nation { public int ID {get; set;} public int name {get;set;} public List<NationAlly> NationAllies {get;set;} } public class NationAlly { public int ID {get; set;} public int level {get;set;} public Nation toNation {get;set;} } I'm using EF 4 and CodeFirst with a DbContext called NationsDB to manage my database on SQL Server 2008. If I create a new object of type Nation and I

Node js and Code First

不想你离开。 提交于 2019-12-09 18:38:29
问题 I've worked on Entity Framework with Code First approach. Now I am learning Node.js I wonder is there a way to make the same code first approach using Node.js and some libraly? I am thinking of using MySql as database. 回答1: You can look into Sequelize. Example of usage from the home page (I added comments to relate to Code First): var Sequelize = require('sequelize'); // define your "context" var sequelize = new Sequelize('database', 'username', 'password'); // define your "entities" var User

Code-First Entity Framework inserting data with custom ID

岁酱吖の 提交于 2019-12-09 17:21:42
问题 I am using code-first EF in my project and face issue when the data with custom id is being inserted. When I am trying to insert data with custom ID (for instance 999), EF ignores it and inserts incremented ID into table. My model: public class Address { [Key] public int Id { get; set; } public string FirstName { get; set; } ... } How to solve this probleb? EDIT: 1) How to begin incrementing from N, but not from 0? 2) If I don't specify custom ID, DB must increment and inserts own. If I

How do you minimize the performance hit when upgrading to EF 4.1 from LINQ to SQL?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-09 13:38:19
问题 I recently updated an app with LINQ to SQL and SQL Server CE 3.5 to Entity Framework 4.1 Code First and SQL Server CE 4.0, and it's now running noticeably slower. I did some before vs. after stopwatch testing, and most major operations of my app appear to be running about 40% slower on average. I'm using all default strategies and configurations for EF Code First except for disabling cascading deletes. When I originally posted this question, I was focused on one query that seemed to be taking

Can I hide my ICollection<T> fields when I have a one-to-many mapping in EF4 code-only?

一世执手 提交于 2019-12-09 12:28:30
问题 My domain classes that have one-to-many mappings generally take the following form (untested code): public Customer Customer { // Public methods. public Order AddOrder(Order order) { _orders.Add(order); } public Order GetOrder(long id) { return _orders.Where(x => x.Id).Single(); } // etc. // Private fields. private ICollection<Order> _orders = new List<Order>(); } The EF4 code-only samples I've seen expose a public ICollection when dealing with one-to-many relationships. Is there a way to

CRUD Views For Many-Many Relationship, Checkboxes

谁说胖子不能爱 提交于 2019-12-09 06:47:23
问题 I am having a hard time trying to figure out what I need to do to get this to work. I'm learning ASP.NET MVC CodeFirst with EF. If I make a model I can simply add a controller for that model and add scaffolding to create views that automatically take care of CRUD. But now I have two models, Project and Category. They have a many to many relationship and database is designed correctly with the associative table without having to make a separate model for it. The code for the models is this....

EF 4.1 Mapping Problem

和自甴很熟 提交于 2019-12-09 05:52:53
问题 I have a class that have a relationship with itself: public class Person { public long ID { get; set; } public string Name { get; set; } public virtual Person Mother { get; set; } public virtual Person Father { get; set; } } When EF 4.1 is trying to map this class I'm getting the follow error: 'Unable to determine the principal end of an association between the types 'Model.Person' and 'Model.person'. The principal end of this association must be explicitly configured using either the

define scalar function with ef4.1 code first?

醉酒当歌 提交于 2019-12-08 20:06:29
i have a function called distancebetween i wnat to define it in scalar valued function to call it by linq if there is away to do that by EF4.1 code first ? No. Code first was designed as "code first" = you will create a code and it will create a database where no database logic exists (no views, stored procedures, triggers or functions). Because of that it is completely missing features for mapping database logic like functions and stored procedures. If you want to use it in LINQ you must abandon code-first / fluent-API and start tu use EDMX where this is supported. Using ExecuteSqlCommand and