"System.NullReferenceException" Error While Using Linq [duplicate] - c#

This question already has answers here:
EF LINQ include multiple and nested entities
(7 answers)
Closed 1 year ago.
I'm trying to learn one to many relations with EF Core.
In summary: I have two tables; Artists and Songs.
But when I try to reach songs which belongs that artist I am getting error.
Why I'm getting null reference error?

You have to include the related entity you want in the list manually, include Songs like the following way:
var theArtist = DB.Artists.Include("Songs").FirstOrDefault(a => a.ID==1);

Related

Getting data by string column names in Entity Framework(Not duplicate they are exactly different question) [duplicate]

This question already has answers here:
LINQ to Entities - Addressing class properties with their string names
(3 answers)
Closed 4 years ago.
I'm using Entity Framework with C#. And I want to getting columns with string column name like below. How can I achieve this?
I want like below
context.Students.Select({
"Name",
"Surname",
"Number",
"BirthDate",
}).ToList();
Please don't advice below solution
context.Students.Select(p=> new {
p.Name,
p.Surname,
p.Number,
p.BirthDate,
}).ToList();
I don't think this is supported out of the box.
You can use context.Students.SqlQuery but then you will need to pass a complete SQL Statement.
Alternatively you may need to create some helper that maps string to properties, but that will involve reflection.

Running a Distinct on a record [duplicate]

This question already has answers here:
Distinct in Linq based on only one field of the table
(10 answers)
Closed 7 years ago.
In LINQ if the result that I have is the a list of whole records from the database and I want to do a Distinct on one column of this record, How do I do that?
You could try something as simple as:
var distincts = records.Select(x=>x.ColumnName).Distinct();

EntityFramework - Cannot Delete Entity using DbSet [duplicate]

This question already has answers here:
The object cannot be deleted because it was not found in the ObjectStateManager in entity framework 5
(5 answers)
Closed 8 years ago.
I am trying to delete an object from my database using EntityFramework's DbSet. The code is as follows:
var dbObject = FindById(id);
_masterDb.DbTable.Remove(dbObject);
_masterDb.SaveChanges();
I get the following error: "The object cannot be deleted because it was not found in the ObjectStateManager."
Please can someone tell me what I am doing wrong?
Possibly the entity is not attached to the same context.
Does this work:
var dbObject = FindById(id);
_masterDb.DbTable.Attach(dbObject);
_masterDb.DbTable.Remove(dbObject);
_masterDb.SaveChanges();

Using linq to select all tables in database [duplicate]

This question already has answers here:
Linq: Get a list of all tables within DataContext
(4 answers)
Closed 8 years ago.
I want to select list of all tables (Not columns) in a database using LINQ dynamically.
I just want it Dynamically not what listed in DataContext static values.For example i alter Table2 after deploying program. in this situation how i should find it.
By the way of there are any query also please let me know.
I think you can use the "Mapping" feature of LINQ:
context.Mapping.GetTables();
if you want to get tables that are modeled you can use #Mygyll answer, but if you want to list all tables in database you can use SMO, in smo when you have a database you can get all tables via this code
db.Tables.Cast<Table>()

CannotDelete Entity in Database [duplicate]

This question already has answers here:
The object cannot be deleted because it was not found in the ObjectStateManager
(10 answers)
Closed 8 years ago.
Here is my Code,
db.Set<T>().Remove(item);
db.SaveChanges();
And i got this error.
{"The object cannot be deleted because it was not found in the ObjectStateManager."}
i tried many ways to fix that problem, but i couldn't. How can id fix ?
thank you all.
Most likely you are trying to remove an object that doesn't come from your DbContext. For example if your object comes from a web form in ASP .Net MVC it has noting to do with the database until you attach it to the database.
There are 2 things you can do to be able to remove your item:
db.Set<T>.Attach(item);
// now you should be able to remove it
or
item = db.Set<T>.Single(x => x.Id == itme.Id);
// now you should be able to remove it
EDIT
You should also make sure that you're not trying to attach an object that already is attached. Take a look at the EntitState:
db.Entry(item).State

Categories