Questions tagged [entity-framework]

31168 questions
1

votes
1

answer
792

Views

In constructors and initializers, only property or field parameter bindings are supported in LINQ to Entities

I am able to execute the below code snippet in my application with no problem. Projects.Select(s => new ProjectName() { ProjectID = s.ProjectID, Name = s.Name }) Note that ProjectName is a parameterless constructor which exists in the following class: namespace YeagerTechModel.DropDownLists { [Dat...
sagesky36
0

votes
2

answer
31

Views

EF foreign key reference using Id vs object

What is the difference between foreign key reference using Id vs object. For example: FK relation using Id class Product { public int Id { get; set; } public string Name { get; set; } public string PhoneNumber { get; set; } public int CategoryId { get; set; } } vs FK relation using object class Pro...
wenn32
1

votes
1

answer
635

Views

Doing Sum() on a set of anonymous type: is it possible?

If I select an anonymous type (select new) from a query: var smsPerGroup = from g in db.Groups select new { GroupName = g.Name, ReceivedSMS = g.Members.SelectMany(p => p.ReceivedSMS).Count() }; It happends that I can't sum() on it: int max = smsPerGroup.Max(g => g.ReceivedSMS); // ERROR! The error...
sports
1

votes
2

answer
477

Views

How to display cross reference values using Entity Framework and view models

A very newbie question about MVC and Entity Framework (using data driven development). I built an EF model off an existing database. I used the EF wizard/tool in Visual Studio, pointed it to my database, and it created all the entity classes and relationships. Assume it is simple database that has...
John Lee
1

votes
2

answer
568

Views

Two db contexts under TransactionScope fails

I am stuck using two db connections with entity framework contexts under a single transaction. I am trying to use two db contexts under one transaction scope. I get "MSTDC not available". I read it's not an EF problem it's TDC which does not allow two connections. Is there any answer for this proble...
bgS
1

votes
1

answer
1.1k

Views

how to implement computed field for model in web api odata using EF 6

I'm creating a web api odata controller and all is well until I try to add a derived field to the model. I don't want the field to exist in the database, and the field does not need to be queryable. Given model; public class Foo { public Int64 Id { get; set; } public string SomeDatabaseSourcedField...
Rickster
1

votes
2

answer
1.1k

Views

EF Fluent API + two column composite uniqueness + autoincrement primary key

I use Fluent API. I don't like annotations. I like to always use an autoincrement as my primary key, in all of my tables. Some of my tables require that two columns, X and Y (where X is not the autoincrement key and Y is not the autoincrement key) have to be unique, ie: there can't be another row su...
sports
1

votes
1

answer
1.2k

Views

Using Entity Framework, should I use RETURN or SELECT in my stored procedures?

I am currently trying to use stored procedures with Entity Framework and I am encountering a problem with return type. The return type of my stored procedure is BIT. Here is the code (simplified) of my stored procedure : CREATE PROCEDURE [dbo].[sp_makeStuff] @id int AS BEGIN DECLARE @status BIT BEGI...
Syph3R
1

votes
1

answer
2.3k

Views

The declared type of navigation property Project.Models.Customer.SubCustomers is not compatible with the result of the specified navigation

I have had a look at answers to similar errors, but I can't seem to find an answer to my particular error. I am running a migration and the error happens when the seed method is run (migration has worked fine). The declared type of navigation property Project.Models.Customer.SubCustomers is not comp...
tekiegirl
1

votes
1

answer
531

Views

Use SQL 2012 FORMAT function with LINQ to Entity Framework

I would like to add the built in SQL 2012 FORMAT function to a static function class to use with LINQ to Entity Framework. I followed this source code to mimic adding a built in SQL function. public static class ExtendSqlFunctions { [DbFunction("SqlServer", "FORMAT")] public static string Form...
goroth
1

votes
1

answer
1.4k

Views

Object reference not set to an instance of an object in TempData

[HttpPost] public ActionResult Create(EmployeeEntry employeeentry) { if (ModelState.IsValid) { EmployeeEntry employeesales = new EmployeeEntry(); employeesales.stock_id = employeeentry.stock_id; employeesales.quantity = employeeentry.quantity; employeesales.total = employeeentry.total; employeesales...
King_Fisher
1

votes
1

answer
1.1k

Views

IQueryable Expression Translation

I am creating an IQueryable that I want to use for a query passed to entity framework. My repository does not expose queryable. var query = new List().AsQueryable().Where(x => x.Property == "argument"); I have a method on my repository that will take in an IQueryable. How do I query my DbSet with...
twifosp
1

votes
2

answer
22.1k

Views

How to use Dropdown list in cshtml

How to use dropdown list in cshtml? I am using below code ,Now it looks like general HTML but problem is not apply the bootstrap styles. @{ List listItems = new List(); listItems.Add(new SelectListItem { Text = "AtlantaPort", Value = "AtlantaPort" }); listItems.Add(new SelectListItem { Text = "NewJ...
Naresh Raju
1

votes
1

answer
80

Views

EF 6.x Code First: Different SQL Generated for Navigation Property

I'm a veteran web developer with plenty of T-SQL and schema designing experience, but, fairly new to EF (skipping right to EF 6!). I'm wondering what's wrong with my POCOs that I'm getting different SQL generated for the following two queries. Basically, I have two objects, "Parent" and "ChildThing"...
bkwdesign
0

votes
3

answer
25

Views

I'm trying to access the list across an entity framework, dump me the error of integrated security

I set up a test web site through which I will access the database, and browse content in the database, on the website I will see the table from the database. I work in Visual Studio 2017, framework is 2.0, writing code is c # 7.1, i've been upgrading nuget package Microsoft.EntityFrameworkCore.SqlSe...
Matejbrcic
1

votes
4

answer
989

Views

sql query builder for select in c#

In my application, I want to build sql query to get result set and export to excel. In my page I need to specify the criteria and the columns which are needed to show in excel. I have gone through this post. In that we can select the required columns and can write where clause, and select from multi...
Jiju John
1

votes
2

answer
1.4k

Views

Eager Loading with Entity Framework, LINQ

I am working on ASP.NET MVC project. i am using EF code-first approach. i have 3 classes, which are: public class A { public int AID {get;set;} public string A1 {get;set} public string A2 {get;set} public virtual List Bs {get;set;} } public class B { public int BID {get;set;} public string B1 {get;s...
Willie
4

votes
1

answer
27

Views

How to work with ASP.Net Core Identity with Web API Core and Angular 6

Please do help me out for this. I am using technologies: Asp.Net core 2.1 Web API core 2.1, Entity Framework core 2.1 Angular 6. We wanted to integrate Asp.Net core identity for Authentication and Authorization, Please do help me out for this, Below is the structure of my project,
Sharad
1

votes
1

answer
878

Views

Linq Expression to handle null

I am trying to use the following code in a linq expression, which I found at this question However it fails if the database field is null. public static IQueryable FieldsAreEqualOrBothNullOrEmpty( this IQueryable source, Expression member, string value) { Expression body; if (string.IsNullOrEmpty(v...
Kirsten Greed
1

votes
2

answer
452

Views

Can't specify identity column explicitly BUT ALSO can't leave it unspecified

I'm hitting a catch 22 here. When I create a new object like so and store it using EF, I get the error. private void DataGrid_OnAddingNewItem(object sender, AddingNewItemEventArgs eventArgs) { eventArgs.NewItem = new Customer { Id = Guid.NewGuid(), Discount = 0 }; } The only identity column in that...
Konrad Viltersten
1

votes
1

answer
2.1k

Views

Cannot use table 'AspNetUsers' in schema '' for entity 'AspNetUsers' since it is being used for another entity

We are trying to add Identity 3 to our existing Customers app by extending AspNetUsers public class ApplicationUser : IdentityUser { public string BusinessName { get; set; } public string ContactName { get; set; } public string CountryCode { get; set; } public virtual Countries Country { get; set; }...
Vague
0

votes
0

answer
17

Views

Best way to search for users in location

I have a problem that I'm trying to solve. I have 3 tables that holds the locations, users, with their relevant Ids. What I'm trying to achieve is to allow an admin to be able to search for users in a particular location. So in short, I'm trying to allow Sam who is the Admin to locate users only i...
Chris H.
0

votes
2

answer
34

Views

More than one record is updated EF-Linq C#

I have the following table: I have several arrays, where I will scroll if the data match to decrease its amount: //DISMINUYE CANTIDAD DE CASILLER public void disminuyeCasiller(string[] codParte, string [] rolls, double[] cantResta) { int size = codParte.Length; for(int i = 0; i < size; i++) { string...
Baker1562
1

votes
2

answer
523

Views

Connect to remote sql server via alias

My connection string is working when I am connecting via IP adresss I have created Alias on remote server, like I did it localy (same ports and names) ,but it doesnt working. And when I put IP addres in connection string, like the one I have posted it is working. Can someone pleas give me some inst...
1392023093user
1

votes
2

answer
2.4k

Views

A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies

I have model contain class order in this action i create object from class order and fill data for it then serialize my object but throw exception A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.Order_5247E51E921C7E989617CBA5719815085C1A2851A...
Asmaa Mohamed
1

votes
1

answer
2.7k

Views

Using navigation properties instead of LINQ Join in WebApi controller

I am using Entity Framework Code First and LINQ to Entities in a Web Api Controller. I have 3 tables that are related with the following relations/ navigation properties: (the model classes are as follows) My Reservation class: [Table("Reservations")] public class Reservation { [Key] [DatabaseGene...
Sorin Vasiliu
3

votes
2

answer
56

Views

EF6 One-To-Many Relationship with 2 tables in Legacy SQL Database

I am trying to configure a One-To-Many relationship where the second table has a reference to the first table. The tables are in an existing legacy Sql Server database. I have a Customer table and a CustomerBranches table like so: I have a POCO that will allow a Customer object to navigate to its B...
victor lopez jr.
0

votes
0

answer
5

Views

Relationship - EF Core

I'm having some trouble to get into EF Core relationship. I didn't know how to search it properly, so I've not found what I need, but I got somewhere. I have these two classes: Expense: public class Expense : Entity { public string Title { get; set; } public string Description { get; set; } publi...
Guilherme Almeida
1

votes
1

answer
789

Views

Can Entity Framework Core run non-query calls?

Unfortunately my EF application has to call stored procedures I am unable to change. While this is not ideal I can normally get around it. However, I have a stored proc that does return anything. How does EF core deal with this? I know in previous versions you could run ExecuteNonQuery but I haven't...
evenflow58
1

votes
2

answer
3.6k

Views

ThenInclude not working for an Entity Framework LINQ query [duplicate]

This question already has an answer here: Can not load related data with Include or ThenInclude or Select/Many with ONE query [duplicate] 1 answer I have a database model like this: public class Customer { public int CustomerId{ get; set; } public int OrderId { get; set; } public ICollection Orders...
Marvin Püthe
1

votes
1

answer
37

Views

Entity framework core can allow to specify two different length for column one for database and other for modal.validation?

Entity framework core can allow to specify two different length of column one for database and other for modal.validation?
chintan310
0

votes
0

answer
24

Views

Inherit from generic class in C#

I have .net core 2.1 project. And my repository classes like below. But because of MyDbContext constructor has parameter, I'm getting error like below. When I remove JwtHelper parametrer, it is working perfectly. But, I need adding JwtHelper in MyDbContext.cs for logging auditings. How can I achieve...
Hasan Ozdemir
0

votes
0

answer
19

Views

How to get SQL Server column size with EF6

How to get SQL Server column size with EF6? Table1 design: Column properties:
Anıl
6

votes
1

answer
82

Views

Bit-Bool on a Entity Framework Scalar Function Throws 'cannot be translated' Exception

I have many working Entity Framework Scalar Function's. However, when I try to return a 'truthy' value through a scalar function I get the following exception: The specified method 'Boolean svfn_CanCloneDocument(Int32, System.String)' on the type 'ETC.Operations.DbClient.DbClient.Data.DbClientConte...
Prisoner ZERO
0

votes
2

answer
14

Views

How to implement dependency injection of db context

I'm trying to adapt this tutorial on how to make a shopping cart in asp.net core mvc but I'm having trouble with the db context. I get the following error: Error CS7036 There is no argument given that corresponds to the required formal parameter 'options' of 'StoreContext.StoreContext(DbContextOpt...
mpho
1

votes
1

answer
988

Views

EF core not creating tables on migrate method

Hey I just started using EF core and everything works fine. I call the the context.Database.Migrate() method and it creates a database. But even though my context object has a DBSet, it doesn't create any tables except for the migration history. Can anyone help me with this issue?
Jajan
1

votes
3

answer
40

Views

Failing test while trying to mock Entity Framework with MOQ

I am writing an ASP.NET console application to practice mocking Entity Framework with MOQ for testing. The app manages a book store and has a basic EditPrice method as shown below: public class BookStore { private BookContext context; public BookStore(BookContext newContext) { context = newContext;...
Eli Rushbrook
0

votes
1

answer
15

Views

Why I'm getting wrong filter result?

I'm using Entity Framework to get the Product entity with Id = 5002. Each Product has a list of Orders. So that I'm filtering the Orders to get only Orders with Total greatter 10.00 but the EF returns me the full list. using (var context = new SuperMarketContext()) { var product = context.Product .I...
Eric Farias
1

votes
0

answer
19

Views

Post request to database associated with primary and foreign key in web api using entity framework

I have a database with three tables with primary and foreign key association .I am using web API and entity framework to auto generate methods .I have been successfully processed n doing that .But I want to insert into table with foreign key separately . Currently , I am able to get JSON object for...
Ayushi Gupta
0

votes
0

answer
20

Views

Entity Framework: Cannot be configured on 'xxx' class because it is a derived type

I got this error when try add "AppUserRoles" table by myself to access UserRoles from User, for example: from u in _userManager.Users.Where(x => x.UserRoles "contain" "some codition here") And I got this error: A key cannot be configured on 'AppUserRoles' because it is a derived type. The key must b...
Trí Chồn

View additional questions