Questions tagged [ef-code-first]

EF Code-First is a way of using Microsoft's Entity Framework with POCO classes, as opposed to model-first or DB-first.

Entity Framework Code-First is a methodology for providing mapping between .NET CLR classes and database structure. Classes and properties may be marked up with attribute decorators (for example [Table("MyTable")] or [Column("CreatedDate")]) or the description for mapping the classes and their properties may be made through the FluentAPI method calls overriding the model creation.

Code-first also operates by convention in that with no markup or FluentAPI coding, it will attempt to connect to a default SQLServer instance to a database named for the project that contains the DBContext class (for example if creating a FoodPantryDAL project where your context will be created, it will attempt to connect to ./SQLExpress/FoodPantryDAL). If the database is not there (and the SQLServer instance is) it will generate the database according to the classes and properties that are currently defined. Tables will be named after the classes they represent, properties also. Properties named ID or [ClassName]ID will be created as a primary key. Classes which reference other classes will be given foreign key relations to those classes, etc.

Code-First may be used not only to create a new database but can all create a mapping to an existing database structure.

8514 questions
3
votes
2 answers

Bind 2 table together (one to many relationship)

I am pretty new to c#, EF Code First and all that, so my question might be an easy one. I am trying to create a very simple login. Each user must have a type (admin or client). How can I bind the usertype to my user table without generating a new…
WaZz
  • 47
  • 5
3
votes
1 answer

Foreign key issue: EF Core 7 code-first: "Cannot insert explicit value for identity column in table '' when IDENTITY_INSERT is set to OFF."

I'm sure the issue is in OnModelCreating but I'm not sure what's missing. I'm trying to create two DbSets, one for SocialMediaUrls with a foreign key to SocialMediaTypes. When I'm trying to insert a row into SocialMediaUrls, the code is also trying…
3
votes
2 answers

How to implement DI using DbContext in Windows Form

I have a class running in a winforms app which uses EF Code First. The DbContext is created via DI through the class constructor. All works well. The problem is the data being referenced is also being modified via a web site, using the same DI…
mattdwen
  • 5,288
  • 10
  • 47
  • 61
3
votes
3 answers

Entity Framework Code First associations/FK issues and assumptions/defaults

I am very confused by the way Entity Framework is able to pick up on relationships between entities. I have a few questions on this. In a simple test application, I have a table of people, and a table of notes, and a table of picture assets. There…
Wil
  • 10,234
  • 12
  • 54
  • 81
3
votes
1 answer

EF-Code First: Unable to create a constant value of type ''. Only primitive types ('such as Int32, String, and Guid') are supported in this context

I am currently having trouble with some some linq using EF code first. My current code works fine: My database model public class MyDatabase : DbContext { public DbSet People { get; set; } public DbSet Roles { get; set; } …
Andrew Beal
  • 427
  • 8
  • 23
3
votes
2 answers

C# Float to Real in SQL Server

I am attempting to use Entity Framework and have a contact database that has Longitude and Latitude data from Google Maps. The guide says that this should be stored as float. I have created my POCO entity which includes Longitude as a float and…
wil
  • 2,019
  • 2
  • 15
  • 14
3
votes
2 answers

Referencing EntityFramework 4.1 namespace in MVC3 Razor View

I am trying to reference the System.Data.Entity.Validation (EF 4.1 version) namespace inside of a shared View in my MVC3 project. I've been able to reference other external libraries using: @using Example.Namespace I cannot, however, get the same…
DMC
  • 361
  • 4
  • 15
3
votes
2 answers

Entity Framework Code First ReadOnly Collections

As a new to Entity Framework, and already having developed almost all classes with the ReadOnly collections, my question is: Is there a way to use ReadOnly collections with Code First? OR Should ReadOnly collections be used with Code First?
Minnie
  • 257
  • 3
  • 16
3
votes
3 answers

Problem with TPH Inheritance mapping in EF 4.1 with Code First

I have these classes in my ASP.NET MVC app: public abstract class Person { public int PersonId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } public class Student : Person { public…
amiry jd
  • 27,021
  • 30
  • 116
  • 215
3
votes
1 answer

Is possible to create a database (Sql Server compact) on a given path if we use Entity framework with code-first approach?

I'm a bit confused about Entity framework, I would like to use code-first approach; infact I would like to write how my database tables are composed through class definition. The main problem is that I need to create a database (or open it) by…
Francesco Belladonna
  • 11,361
  • 12
  • 77
  • 147
3
votes
1 answer

Entity Framework 4.1 Code First - How to create two different relationships between two tables

I need to create a relationship where I have a user table that links to an address table. The problem is that I need the address table to also store historic addresses. It is also possible that a user might not have an address at all. public class…
3
votes
2 answers

asp.net mvc 3 validation for Id field with EF code first

I have the following model: public class Product { [Key] [HiddenInput(DisplayValue = false)] public int Id { get; set; } [Required] [StringLength(10)] public string ProductCode { get; set; } [Required] [StringLength(40)] public…
Marcel Popescu
  • 3,146
  • 3
  • 35
  • 42
3
votes
3 answers

Miniprofiler 1.9.1 throws Unable to determine the provider name for connection of type 'System.Data.SqlClient.SqlConnection' with EF Code First

I know I am probably being a bit thick however...... Created a new MVC3 test application using EF code first. Context: public class EmployeeContext : DbContext { public DbSet Employees { get; set; } } Controller: …
Sean Kenny
  • 1,626
  • 13
  • 14
3
votes
2 answers

MVC 3 EF Code-first to webhost database trouble

Im fairly new to ASP.NET MVC 3, and to coding in general really. I have a very very small application i want to upload to my webhosting domain. I am using entity framework, and it works fine on my local machine. I've entered a new connection string…
3
votes
1 answer

Entity Framework 4.1 Code First: Single column foreign-key to multiple entities

I've been trying to create model in EF 4.1 to represent a database schema with a single table and column holding foreign keys from two other tables, but have had little luck with both annotations and the fluent API. A sample model is shown…
Brent
  • 411
  • 4
  • 11