Questions tagged [ef-code-first-mapping]

0

votes
0

answer
12

Views

Convert Dictionary to ICollection in Entity Framework Core

Here I am stucked with the conversion of dictionary to Icollection in EF Core. I have Dictionary in FlatEmployee class in which I am storing list of key, value pair in database. I have declared like this: public class FlatEmployee { public int EmployeeId { get; set; } public Dictionary PayAndAllowan...
Iswar
1

votes
2

answer
124

Views

EF Code First - How to Deploy to Azure

I've been struggling with this all day. I can't make my WebApi to access my database. I'm using the Code First approach with Entity Framework. When I'm deploying, a new connection string is added to web.config. And this raises an exception: This is what I've got in my web.config (locally): And thi...
RottenCheese
1

votes
0

answer
628

Views

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

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...
Reshmy
1

votes
1

answer
76

Views

How to order selected data based on nested table in ASP.NET CORE Code first

I have three tables Category, Article, and Comment, each category has many articles and each article has many comments. I want to select all categories ordered by count of comments in all articles in each category. I can only select all categories ordered by articles count like that: var query = con...
mustafa
1

votes
0

answer
485

Views

How to create user defined functions using code first in ABP?

I want to create some user-defined functions using code first approach in abp framework. I have created stored procedures xyz by adding xyz.sql inside EntityFrameworkCore\Migrations\Stored Procedures folder, But not able to find a way to create a function, please help.
vivek nuna
1

votes
0

answer
219

Views

Entity Framework 6 filtered index

I'm developing an application using Entity Framework 6 and I want to use filtered indexes (index with where clause). In Entity Framework there is an Index annotation which makes it possible to define an index on a property (column). I define something like public class SqlDefaultValueAttribute : Att...
hamed
1

votes
1

answer
83

Views

Update-Database connects to wrong database

I set connection string in my Startup.cs (Startup class and ConfigureServices method). Sample code below: if (_currentEnvironment.IsProduction()) { connectionString = 'ProdConnection string here ..'; } else { connectionString = 'Devl connection string here ...'; } In the Package Manager Consoler of...
user3885927
1

votes
0

answer
39

Views

How to get Entity Framework to map the correct Foreign Key relationships when creating tables

I have the following Administrator class and a AdministratorImage class with a foreign key relationship as shown in the classes. public class Administrator { // Pk public Guid AdministratorId { get; set; } //Properties public string Name { get; set; } public string MiddleName { get; set; } public s...
Ntu
1

votes
1

answer
33

Views

How to retrieve a tree of objects from a stored procedure?

I have a calculation that I'd like to do in a stored procedure, which includes some aggregations, to reduce the amount of data sent back to the caller. Given the structure: CREATE TABLE Parent ( ParentID int NOT NULL PRIMARY KEY, A1 int NOT NULL ) CREATE TABLE Child ( ChildID int NOT NULL PRIMARY KE...
Mike Dimmick
1

votes
2

answer
231

Views

Entity Framework TPT - Insert sub-type for existing base-type

I am running into a problem where I want to insert a record for a sub-type with an existing record for the base-type. The base-type may be a Person entity described by: public class Person { [Key, Column('Id'), DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public D...
rick
1

votes
2

answer
37

Views

Convert SQL code to linq (LIKE, SELECT)

How can I do the following sql code with linq? SQL; SELECT BusinessEntityID, FirstName + ' ' + LastName AS 'FullName' FROM Person WHERE FullName LIKE 'a%' LINQ; using(var db= new db_Context) { var query = db.Person.Select(q=> q.FirstName + ' '+ q=>q.FullName) }
yuksel
1

votes
0

answer
45

Views

How to add a reference to a user in my entity in EF

I have the following classes (Simplified): public class Company { public Guid Id {get;set;} public List Farms {get;set;} public List Employees {get;set;} } public class Farm { public Guid Id {get;set;} public Company Owner {get;set;} } public class User { public Guid Id {get;set;} public string Name...
SEG.Veenstra
1

votes
0

answer
54

Views

Entity Framework 6 (code-first) - composite foreign key

I'm working with Entity Framework (code-first) for the first time and I can't figure out how to accomplish a specific foreign key. My code looks something like this: public class Customer { [Key] [Index('IX_Customer_Entity', IsUnique = true, Order = 1)] public int CustomerID { get; set; } [Index('IX...
Berrepoot
1

votes
0

answer
66

Views

Entity Framework Working With Dynamic Table Names

I created a database infrastructure with EF code-first like this: Main_Table1 Main_Table2 Sub_XX_Table1 Sub_XX_Table2 I want XX to be filled from the logged-in user (and Entity Framework should create tables automatically if they do not exist). Is that possible? I tried to use TableAttribute, but th...
1

votes
1

answer
157

Views

Entity Framework Core Mapping Multiple objects to an object in one to many

I am trying to work out the best way to handle a Note object that is linked to multiple objects i.e. Contacts, Quotes. These objects can have many Notes, A Quote has many Notes. public class Note { public int Id { get; set; } public string Title { get; set; } public string NoteDetails { get;...
Gweilo
1

votes
0

answer
50

Views

data is not dispalying in postman in .net core

This Is Model named as Student using System; namespace peakR.Model.DBModels { public partial class Student: BaseEntity { //public long Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Gender { get; set; } public DateTime? DateOfBirth { get; se...
abhay bhadoriya
1

votes
2

answer
74

Views

The INSERT statement conflicted with the FOREIGN KEY, but debug window shows correct values

Hello guys I am new with asp.net mvc ANDcurrently working on my college project which is a college portal and using entity framework code first approach. However I am getting an error ** The INSERT statement conflicted with the FOREIGN KEY constraint 'FK_dbo.Notices_dbo.Colleges_CollegeId'. The conf...
IndusDev
1

votes
0

answer
319

Views

EF Core Include multiple inherited types TPH

The problem Query BaseClass and Include information from derived types. For example: Base class Offer has two types one with a Card only (CardOffer) and with Multiple Cards (CarouselOffer). Question: How to query the BaseClass and Include the information from the derived types CardOffer and Carouse...
rinormaloku
1

votes
0

answer
122

Views

Disable delete cascade on one to many in same table Entity Framework core

This is my table, so an user can have a manager which is also an user : public class User : IdentityUser { public string FirstName { get; set; } public string LastName { get; set; } public int? AttachedManagerId { get; set; } public ApplicationUser AttachedManager { get; set; } } And this is to try...
Lucas Weibel
1

votes
1

answer
92

Views

NPGSQL date time array

I'm attempting to use the timestamp[] field type in Postgres with NPGSQL, so that I can use the DateTime[] type in my Entity Framework models. I've added this to my EF code first model. [Column('HostUnavailableDates', TypeName = 'timestamp[]')] public DateTime[] HostUnavailableDates { get; set; } I'...
Chris
1

votes
1

answer
24

Views

Prevent EF Core from dropping tables in DB

I created a new project in VS 2017 with template authentication by individual accounts. So I had initial migration with code generating required tables for storing account info. The problem is that once the migration done, with subsequent migrations those created tables are viewed as unwanted and su...
Grumpy Android
1

votes
0

answer
35

Views

Inheritance at EF code first create an unexpected cast at runtime

I have this: public class Person { [Key] public Guid ID { get; set; } public string Name { get; set; } } public class Student : Person { public int Number { get; set; } public Account AccountInformation { get; set; } } Update: public class Account { [Key] public Guid ID { get; set; } [Require...
MTSS
1

votes
1

answer
62

Views

EF Code First can't seed database [Console app]

I have a console app, I managed to create database using Code First but I can't seed any data. I use update-database in Package Manager Console to do it. Please help. I'm searching for hours. Here is my code: /Models/ApplicationDbContext class: namespace ToDoApp.Models { public class ApplicationDbCo...
Daniel
1

votes
0

answer
170

Views

EF Core Loading One-To-One Entities Circular dependency

I am trying to create an API and I'm having difficulty with a One to One Relationship in EF Core 2.0. One Person Has to have One Car, and One Car has to have One Person. I have two endpoints /Person and /Car. When I perform a get request to /Person I get a list of all people, and those people have...
Reuben Mallinson
1

votes
2

answer
139

Views

Entity Framework Code first self referencing ID inserts wrong entity

I'm having trouble inserting into a table with code first using EF Core 2.0. I get this exception when calling my context.SaveChanges() Cannot insert explicit value for identity column in table 'Departments' when IDENTITY_INSERT is set to OFF. Here is my Model: public class Department { public int...
Matt Baech
1

votes
0

answer
95

Views

EF Map list property to single column in different table

I have the following situation: I have a table 'Product' which has Id | Name Then I have another table Product_Vendor which has Id | Product_Id | Vendor_Id where Product_Id is a foreign key to Product.Id. Now in my Entity class code, I want the Product entity to have a property List Vendors { get;...
1

votes
0

answer
139

Views

Edit or update the payload in a many-to-many joint table: change or assign a grade on the Contoso University example

I am writing an ASP.Net Core MVC with EF application based on the Contoso University series of tutorials: https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro?view=aspnetcore-2.1 In the Create a complex data model tutorial (ASP.NET Core MVC with EF Core - Data Model - 5 of 10) there is an...
1

votes
1

answer
527

Views

EF Core GUID Database Generated in Abstract base class

So my entities inherit from a class like this: public abstract class EntityBase { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid UuId { get; set; } [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [Timestamp] public byte[] RowVersion { get; s...
Jammer
1

votes
1

answer
44

Views

List does not show up in binding source

I have the following collection in my database: public class Customer { public int Id { get; set; } public string Name { get; set; } public List Medications { get; set; } } public class Medication { public int Id { get; set; } public string Name { get; set; } } However when I bind the Customer data...
Kuseq
1

votes
0

answer
18

Views

EF6 Code First HasForiengKey renames columns

I have a .net/database project where records are uniquely identified by a version id and a numerical key (taken from a SEQUENCE). I use EF code first (in fact, code only, because the database must not be modified by code) with POCOs and relations. All relations are specified through modelBuilder act...
Guenter
1

votes
0

answer
59

Views

Bind to viewmodel collection of dynamiclly created inputs

Having this complex model: public class Food { public int FoodID { get; set; } public string FoodNameEN { get; set; } public string FoodDescriptionEN { get; set; } public int CategoryID { get; set; } public bool Availability { get; set; } public bool DailyMenu { get; set; } public virtual Diet Diet...
Flamur Dervishi
1

votes
1

answer
110

Views

C# Code-First not recognizing data annotations

When I use Update-Database, it generally runs just fine and I see my changes on the SQL side. When I created the model, all properties used Required, but after removing this annotation, the mapped column is still not null. I've tested my migration setup by adding and removing columns, this works f...
CrazyPaste
1

votes
1

answer
39

Views

SSIS - Entity Framework - {Model}.resource.dll is missing

I´m writing a SSIS Package with a Script Component. In this script component I´m trying to etablish a Entity Framework Connection, but I´m getting following error: Could not load file or assembly '{Model}.resource.dll' or one of its dependencies error {Model} is the dll containing my EF6 Code fir...
RayUp
1

votes
0

answer
46

Views

EF setting 1 to 0..1 Navigation Property throws exception

I have a model where a Person can have an Key associated but a Key must have Person (cascade), modeled by following code-first model with Navigation Properties: class Key { [Key] public string Code { get; set; } [Required] public virtual Person Person { get; set; } } class Person { [Key] public int...
wondra
1

votes
1

answer
86

Views

Entity Framework Core Code First changing added entities values before save causes identity insert error

I got a problem with setting values before saving an added entity. When I don't change the entities values it saves without a problem. The error I get is this: SqlException: Cannot insert explicit value for identity column in table 'TableName' when IDENTITY_INSERT is set to OFF. I am using .net Core...
Chris
1

votes
0

answer
43

Views

Entity Framework, Code-First, Mapping linking table

I'm writing a WebAPI RESTful service backed onto a MySQL database using C# VS2017. Let me start by stating that I have been unable to get the MySQL integration working correctly to implement Datbase-first design, so I'm going the other way round and implementing code-first. I've tried following all...
Hypnodude
1

votes
0

answer
75

Views

Primary Key Discriminator persisted with default value in EF Core

Background I'm using code first with mapping classes in EF Core at my current project. One of my entities, Action is an abstract class that has multiple inheritors ActionOne, ActionTwo, and ActionThree. I'm using TPH with an enum discriminator ActionType, which has values starting with 1 and up (it'...
Shay Nissel
1

votes
0

answer
143

Views

Entity Framework 6 Code First Migration not working with Oracle Database

I am developing an ASP.NET MVC 5 application with EF6 and Oracle database. I am using the code First approach, I created my entities and My Context class and then I tried the Enable and Add a Migration but I got this error. However, If I use SQL SERVER in the place of Oracle database it works fine...
Haytham
1

votes
0

answer
30

Views

EF Code First Fluent API specifying product, class relationship

I am trying to create a composite foreign key (if that is the correct term) which enforces a product class type of relationship on a table where the product is mandatory but class is not. I would like to use Code First and the Fluent API to achieve this model. The scenario is: I have commodities wh...
Tony
1

votes
1

answer
31

Views

Is Read Committed Snapshot with EF Savechanges causing race condition

I'm having an issue with EF6 using a Code-First approach where a new DB Context attempting to access an entity is causing a null reference exception. Here is an example of my scenario: Main thread int personId = 0; using(var ctx = new exampleContext()) { var person = new Person() {Name = 'Alex'}; ct...
Alex Palacios

View additional questions