code-first

Entity Framework - The foreign key component … is not a declared property on type

杀马特。学长 韩版系。学妹 提交于 2019-12-04 23:49:28
I have the following Model public class FilanthropyEvent : EntityBase, IDeleteable { public int Id { get; set; } public string Name { get; set; } public DateTime EventDate { get; set; } public string Description { get; set; } public decimal Target { get; set; } public decimal EntryFee { get; set; } public bool Deleted { get; set; } public ICollection<EventAttendee> EventAttendees { get; set; } } public class Attendee : EntityBase, IDeleteable { public int Id { get; set; } public string Email { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public bool

DbContext's ChangeTracker problem

本秂侑毒 提交于 2019-12-04 22:55:51
I have a codefirst EF-4.1 based program. The user gets a context and can modify some properties. When the user is done, I do a quick ChangeTracker.Entries().Any(e => e.State != EntityState.Unchanged); to determine if a SaveChanges() is required or not. If I do a 'SaveChanges()' call, the changes that have made are persisted to the database. This works for some properties, and doesn't work for others. Specifically it seems to work with simple types ( float s), and with collection hierarchies( ObservableCollection s). Am I doing something wrong? Ladislav Mrnka Yes this is a problem. Some

What causes “Incorrect syntax near <stored procedure name>” in EF Code First and SQL 2005?

冷暖自知 提交于 2019-12-04 21:58:00
问题 The examples for System.Data.Entity.Database.SqlQuery method I've found appear to work well with SQL 2008 R2 but do not appear to work with SQL 2005. This call will work with SQL 2008 R2: var myEntities = dbContext.Database.SqlQuery<MyEntity>("GetDataFromMySp @EntityId = {0}", entityId); However, in SQL 2005 this statement will throw a SqlException with an error message of "Incorrect syntax near 'GetDataFromMySp'". 回答1: Solution found by @Dan himself (couldn't post due to rep) The solution I

Table Mapping problem

老子叫甜甜 提交于 2019-12-04 18:20:38
I am using entity framework 4 in my current project, to read data from several table. Compare using ADO.net, it EF is very easy, with simple code that can do lots of work or me. But there is one problem... e,g there is and exiting table call Table "MTable" i only want to query two column from this table, however this table is share with other two ppl who are also working on it. they might add column or modify constrains on this table. Only one thing i am sure is that, the two column i want to query they wont delete it or rename it. My application is runing now, but from time to time it break

Entity Framework Code First: One-to-Many and Many-to-Many relationships to same table

两盒软妹~` 提交于 2019-12-04 18:14:05
问题 I have a User model and a Event model in my project. The Event has a creator(User) and has participant(Users) so Event has a one-to-many relationship with User and also a many-to-many relationship to the same table. I had first the one-to-many relationship like this: Public class Event { ... public int CreatedById { get; set; } public virtual User CreatedBy { get; set; } ... } Then when I added the many-to-many relationship the migration doesn't generate the many to many relationship: Public

How to add a column to a link table with code first

大憨熊 提交于 2019-12-04 17:52:19
I have a ResearchDatabase entity that has a many-to-many relationship with a Subject entity. i.e. a Research Database belongs in one or more Subject Categories, and a Subject Category contains one or more Research Databases. In my ResearchDatabaseConfiguration file I have the following mapping: HasMany(rd => rd.Subjects) .WithMany(s => s.ResearchDatabases) .Map(m => { m.MapLeftKey("DatabaseId"); m.MapRightKey("SubjectId"); m.ToTable("DatabaseSubjects"); }); OK, no problem. Works. Creates a link table. Now I have received a new specification. The ordering of the Research Databases in each

Why is MVC3 not scaffolding my foreign key columns

瘦欲@ 提交于 2019-12-04 17:24:53
I'm trying to use MVC 3 with EF 4.1 using code first and am following Scott Guthries tutorial http://weblogs.asp.net/scottgu/archive/2011/05/05/ef-code-first-and-data-scaffolding-with-the-asp-net-mvc-3-tools-update.aspx . The issue I'm having is that when I create the products controller and the related scaffolded views, there is no "category" column being created in any of the views ("edit", "create", "index" etc), which according to the tutorial should be created. I've traced the reason why the column is not being shown is because of the t4 templates... it is failing a check to see if it is

Keyword not supported: 'data source'.: EF code-first using ObjectContext and LocalDB

╄→гoц情女王★ 提交于 2019-12-04 15:48:58
I am getting a "keyword not supported error" when I try to connect to a LocalDB database using ObjectContext. This is my connection string: <add name="connStr" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=proj1db;Integrated Security=True" /> and this is the code that tries to create an instance of the ObjectContext: var connectionString = ConfigurationManager .ConnectionStrings["connStr"] .ConnectionString; ObjectContext _context = new ObjectContext(connectionString); The last line throws System.ArgumentException: Keyword not supported:

How to configure multiple Foreignkey referancing to same table from main table using EF Code First Fluient API

给你一囗甜甜゛ 提交于 2019-12-04 15:28:41
I have to design a web application using existing database of desktop application. As per existing database i have below class public class Company { #region Primitive Properties public virtual int CompanyID { get; set; } public virtual string CompanyName { get; set; } public virtual bool IsCustomer { get; set; } public virtual string CustomerCode { get; set; } public virtual bool IsPotentialCustomer { get; set; } public virtual bool IsSupplier { get; set; } public virtual string SupplierCode { get; set; } public virtual bool IsPotentialSupplier { get; set; } public CompanyCategoryCodes

Why is my DbContext DbSet null?

走远了吗. 提交于 2019-12-04 14:57:39
问题 I created a new Entity Frameworks Code First app and the DbSet (People) is returning null. public class Person { public int Id { get; set; } public string Name { get; set; } } public class Repository : DbContext { public DbSet<Person> People; } web.config : connection string <connectionStrings> <add name="Repository" connectionString="Data Source=|DataDirectory|Repository.sdf" providerName="System.Data.SqlServerCe.4.0"/> </connectionStrings> Now when I call Repository _repo = new Repository()