Questions tagged [entity-framework]

42464 questions
1

votes
0

answer
39

Views

How to share an InMemory Db between a read and write context

I have am working on a project that needs to support a read db and write db. I have decided to create two contexts accordingly. I have a Web API to interact with the db. I have an endpoint creates a user and another that fetches a user by id. The issue I'm running into is on the Development server,...
czifro
1

votes
0

answer
648

Views

EF how can I refresh Context with updated values in the Database?

I have a static Context that is doesn't refresh after updating values from the DB. Here is a little background on how the systems work. CRUD is done in a separate app. After saving to the DB triggers notify the API on the changes. After getting the changed Entity from the Context in the API, the en...
Ortho Home Defense
1

votes
1

answer
1.4k

Views

.NET Core 2.0 API with EF6 and .edmx file?

I want to build a API using .NET Core 2.0 that targets the full .NET framework so I can work with Entity Framework 6 and an .edmx file. Is this possible? How do I inject the DbContext generated by the .edmx file? I want to do this because my project works with a lot of stored procedures and I don't...
genzop
1

votes
1

answer
58

Views

Entity Framework - mapping one class to two tables with many to one relation

I have a class which represent a connection of a page and a tag and it looks more or less like this: public class TagLink { [Key] public int Id { get; set; } public int PageId { get; set; } public int TagId { get; set; } public string TagName { get; set; } } In my database I would like to have 2 tab...
krajol
1

votes
1

answer
197

Views

Entity Framework Core 2.0 Recreating all DB Tables

Every time I run dotnet ef migrations add the migration created recreates all the DB tables. When then trying to run the migration I obviously get a message that the table already exists. What am I missing? I am using the following packages; I also have this in the csproj;
Christopher Penkin
1

votes
1

answer
259

Views

What's the best way to implement roles in a multi-tenant .net project?

My application is .NET 4.7 Web Api with Entity Framework 6. This is for a software as a service tool I am building. In my application there is a many to many relationship between AspNetUser and Company. A user can have many companies, and a company can have many users. A user needs to have a diffe...
Suren
1

votes
0

answer
57

Views

One or more of the foreign-key properties is non-nullable EF6

I have been trying for a bit to see where the flaw in a method that I created that Updates (including Add) an object and it's children using Entity Framework 6.0. I have looked at all the other SO questions and am not able to see where I am going wrong. I have a Product Entity which can have a list...
Isaac Levin
1

votes
0

answer
150

Views

Performance Improvement with Entity Framework 6 compiled queries

From the Compiled Queries documentation: Starting with the .NET Framework 4.5, LINQ queries are cached automatically. However, you can still use compiled LINQ queries to reduce this cost in later executions and compiled queries can be more efficient than LINQ queries that are automatically cached. N...
user3723486
1

votes
1

answer
407

Views

Entity Inheritance in DDD

I am trying to design a Domain Model of an E-Commerce system using DDD guidelines. I have a situation where I have 2 different types of Products. One product is 'Item' and the other product is 'Package' (Group/Bundle of Items). My domain model so far looks like this public abstract class Product : I...
nafr1
1

votes
1

answer
52

Views

Switchable connection strings via code at run time

I have a WPF application. It has many blocks that access the database. The connection string is determined by a setting in my App.config file. using (var dbContext = new MyEntities()) { // ... } But now I need the ability to switch between different databases at run time. I've updated the code to ma...
Jonathan Wood
1

votes
1

answer
507

Views

Combine ASP.NET MVC and Web API projects using identity

I am new to ASP.NET and I have to create an ASP.NET MVC project using the identity framework for authentication and authorization, but the access to the database (SQL Server) must be done in an independent web service project so I created a Web API project with also identity containing all services...
sam
1

votes
3

answer
503

Views

How to build connection string from existing connection for new database created dynamically?

I have query to dynamically create database. private void ExecuteNonQuery(string sql) { using (var connection = new SqlConnection(_connectionString)) { connection.Open(); SqlCommand command = connection.CreateCommand(); command.CommandText = sql; command.ExecuteNonQuery(); } } private void CreateDat...
Sachin
1

votes
1

answer
65

Views

.NET Using Another Unrelated Model In Razor View

Little context: I have a link model which has a foreign Key ID to the identifier model and client model. The client model and identifier model have the link model as a virtual Icollection. Both the Client model and the identifier model have a foreign Key ID to another model MJTopics. I'm using EF6 t...
liamcook
1

votes
1

answer
110

Views

LINQ query to find related data [duplicate]

This question already has an answer here: Projecting self referencing multi level Entities In Entity Framework 6 2 answers I got some help with my recursive product category tree view here on Stack Overflow before, and this is working: Entity model: public class ProductCategory { public int Id { g...
Stian
1

votes
0

answer
362

Views

CRUD operations in ASP.NET MVC through Entity Framework Code First Approach using stored procedures?

I'm new to ASP.NET MVC and Entity Framework. I have assignment of performing CRUD operations using EF code-first approach via stored procedures. I had tried it in a database-first approach and succeeded, but in a code-first approach, I've been stuck for the past week. I've searched on google but ev...
Awais Zafar
1

votes
1

answer
148

Views

Multiple Grouping with IQueryable and returning a list of objects in C# linq query

I have a table ot_request like in this link. I have to return a list of ot_request entity class with the following conditions. I have to filter the list where the status_id==5 After the filtering, I have to group the rows by their project_id After the grouping by project_id, there will be some creat...
Abdullah
1

votes
0

answer
190

Views

Can not add migration in Entity Framework

Added a new field to RegisterViewModel: [Required] [Display(Name = 'Name ')] public string Name { get; set; } Added in AccountController: if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email, Name = model.Name }; var result = await UserManager.Create...
Maksims
1

votes
0

answer
405

Views

Exception from HRESULT: 0x80131500

I am trying to run this command 'dnx ef migrations add setup' in the cmd of my project. I was returned with this error 'Exception from HRESULT: 0x80131500' and it states it fails to load one or more dependencies. I am using VS2015 Project. Anyone knows what the error? Thanks in advance!
thompsonrapier
1

votes
1

answer
42

Views

Splitting EDMX Model

I'm looking to split my EDMX models into their own project to be referenced across the different projects without having to recreate the models across projects. I have one project that contains the Entity Data Model. I have another project that will contain common items across the projects. This com...
Tsukasa
1

votes
0

answer
118

Views

How to enable entity framework in a project in visual studio 2013 C# mvc project?

I am trying to enable entity framework in visual studio 2013. My project is a c# mvc project which is newly created. I have installed entity framework using package manager. Now when I am trying to enable the entity framework, I am getting this error PM> Enable-Migrations No context type was found...
jhon
1

votes
1

answer
2.1k

Views

Change tracking in Entity Framework Core 2.0 changed my code behavior

That code worked fine in Entity Framework 1.0 could also be 1.1. var pupilFound = await context.Pupils.SingleOrDefaultAsync(p => p.Id == pupil.Id); if (pupilFound == null) { throw new BadDataException($'{nameof(pupil.Id)} is not valid'); } if (pupilFound.UserId != userId) { throw new NotAuthorizedEx...
HelloWorld
1

votes
0

answer
143

Views

Customising ASP.NET Dynamic Data with ADO.Net Entity Data Model for Ready Only and Audit

I previously followed the below tutorial on Dynamic Data scaffolding with Entity framework: https://www.youtube.com/watch?v=hWatePj2RmE (Part 1 and Part 2) (ASP.net Scaffolding, Dynamic Data with Entity framework) I used this tutorial to implement a table editing solution, that does the following: D...
DIG
1

votes
0

answer
57

Views

How does EF instantiate the DbContext property? Reflection?

How does EF instantiate the DbContext property? I mean, you have your context like public class ProductContext : DbContext{ public DbSet Categories { get; set; } public DbSet Products { get; set; } } Greate, than you use it like using (var context = new ProductContext()) { //context.Categori...
P. Waksman
1

votes
0

answer
56

Views

Data Annotation and EF6

I working on wpf application with syncfusion datagrid (SfDataGrid). The grid uses a data-annotation for autogenerated columns. The model is prepared by EF6. I'm trying to set a column header. This code works (header showing): public partial class ViewShortConsumer { [Display(Name = 'Heade...
Gennady Vorobev
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
0

answer
246

Views

Visual Studio DB-connection not showing up in Entity Data Model Wizard

I've opened a database connection through the Tools-menu. It's working without problem and showing me my database and it's tables. However, following this tutorial, I should be able to select my connection in the connection-dropdown the wizard shows me. The dropdown is empty, so I tried creating a n...
Ciphra
1

votes
0

answer
251

Views

Using Simple Injector with EF in WPF app

In my wpf app I need to be able to read/write data from specific table/tables, so, as I'm using SimpleInjector to use all services and stuff. So, basically I want to be able to use repository in some of my services. First I found this How to use Simple injector, Repository and Context - code first A...
DanilGholtsman
1

votes
0

answer
67

Views

Refresh user's permissions after removed from a role

I have a project where I can assign users to roles. My problem is that when I remove a user from a role, the user will be able to access the resources that he is no loger supposed to see. The only way to refresh his permissions is when he log off and login again. This is a problem because when I wan...
pitaridis
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
1

answer
233

Views

Structuremap - dependency injection - EF Core DBContext lifetime per request

Has anyone had any luck setting up an EF Core DBContext with Structuremap 'correctly' (what is correctly?) DBContext needs to be a singleton across the lifetime of the request. I understand that the default lifecycle is Transient. I understand that that will get a nested container, which effectively...
jenson-button-event
1

votes
0

answer
139

Views

Entity Framework generates an inner join and an outer join for the same entity

I have the following models. public class Table1 { public int Id {get; set;} public string Table1Property {get; set;} public int? Table2Id {get; set;} public virtual Table2 Table2 {get; set;} } public class Table2 { public int Id {get; set;} public string Table2Property {get; set;} public virtual IC...
Eremencu Adrian
1

votes
3

answer
446

Views

ASP.NET Web API 2 - Nested Object in Ajax Post coming across as null

I'm attempting to write a MVC/Web API page, and am stuck on the Web API post portion when doing more than just base types. I cannot seem to find an answer why the Roles object keeps getting set to null even though it's posting from the client side (and it's passing the ModelState.IsValid portion?)...
RocCityExpat
1

votes
1

answer
101

Views

Entity Framework Database First get only active rows by default

We recently added a new boolean column ACTIVE to our Employee table. Therefore, when a user wants to delete an employee in the table, the record will be 'soft deleted' (ACTIVE field will be set to false instead of the row being removed completely.) However, the table is being used everywhere in the...
q t
1

votes
0

answer
42

Views

Entity Framework - database first - intercept when EF model update is completed from the database

Here is an interesting scenario: I use the database-first scenario (reasons exist). I update the model from the database often. I use custom .tt files to create my classes. How do I tie into this process to be able to run a custom tool right after the EF model update is complete? I have all figured...
hWright
1

votes
2

answer
81

Views

How to bind and save an object containing a list of objects to database context in ASP.NET MVC with EF code first?

I am new to ASP.NET MVC. I was trying to build a model as follows (assume the syntax is alright). I wanted to know how to bind and save an object that contains list of objects. For eg. in the following code, I want to save an object of type SchoolGrade to the database (it works fine if it does not c...
rakshithnrr
1

votes
0

answer
34

Views

Entity Framework, migrations doesn't include my models

With such ApplicationDbContext: using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using CSharpProject.Models; namespace CSharpProject.Data { public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext(DbContextOptions options) : bas...
mathew
1

votes
2

answer
591

Views

How to remove circular reference from entity framework core

Web Api Core, Entity Framework Core Situation - Many to many relationship. A, Join, B. Join has Id references to both A and B. A has collection for Joins. B has collection for Joins. Goal - Get A with Joins with B - and no further. Problem - I get A with Joins with B with the same singular Join...
user1904662
1

votes
1

answer
71

Views

DbContext returns wrong id after add

I have Pin Model public class Pin { [Key] [Column('pin')] public int Id { get; set; } [StringLength(50, MinimumLength = 1)] [Column('desc', TypeName = 'varchar')] public string Description { get; set; } } I have MyDbContext public class MyDbContext : DbContext { public MyDbContext() : base('DbName')...
Tim
1

votes
1

answer
61

Views

Dynamic Entity Navigation Property

I have an entity called Asset, similar to below: public class Asset { public int Id { get; set; } public int TypeId { get; set; } public int AddedById { get; set; } public DateTime DateTimeAdded { get; set; } public virtual AssetType Type { get; set; } public virtual ITUser AddedBy { get; set; } } I...
Kramb
1

votes
1

answer
65

Views

Only two properties of the model are updated

I defined entity presented below: public class Customer { public int CustomerId { get; set; } [Required] public string FirstName { get; set; } [Required] public string LastName { get; set; } [Required] public string PhoneNumber { get; set; } [Required] public string Email { get; set; } public IColle...
pw94

View additional questions