Hello i am retrieving a IEnumerable<Something> using Entity Framework and this Something has an IEnumerable<SomethingElse> property that has the attribute [NotMapped] over it.
Why does entity framework populate this IEnumerable<SomethingElse> field with some unknown object that is neither null but can't be interogated with Linq throwing the following exception:
System.ArgumentNullException: 'Value cannot be null. (Parameter 'source')'
public class Something
{
[NotMapped]
public IEnumerable<SomethingElse> Items{get;set;}
}
public struct SomethingElse
{
//some fields
}
Insertion:
When i insert the list some elements have the NotMapped field null, while others not:
List<Something> toBeInserted=new List<Something>{ new Something{Items=null},new Something{Items=new[]{ new SomethingElse{}}};
//dbcontext insert the list....
Retrieval
List<Something>somethings=(await this.[SomeDBContext].Somethings).ToList();
Now at retrieval some elements have the NotMapped property null as it should be,while someothers have this weird non-null IEnumerable<SomethingElse>d_6that throws exception on any Linq interogation:
var weirdElement=somethings.First();
weirdElement.Items.Count(); //throws
weirdElement.Items.Any();throws
P.S I have checked this post regarding computed properties and it says: Materialize the list when you fetch it from the database to resolve the computed properties.That is what i have done.
I issue (await this.DBContext.Somethings).ToList() to no avail.
Some Something's still have their IEnumerable<SomethingElse> property not null.
Later Edit
This is a picture with what the
not mapped property of the element looks like:
I am computing the IEnumerable<SomethingElse> using an extension method:
public class Extension
{
public static IEnumerable<SomethingElse> Compute(this IEnumerable<SomethingSomethingElse> someOthers){}
}
The name of the type of the field IEnumerable<SomethingElse> is <Compute>d_6
Related
I have an Entity with a Date property and want to create a boolean property to check if that Date is in the future or not.
public DateTime Date { get; set; }
public virtual bool IsUpcoming {
get
{
return Date >= DateTime.Now;
}
}
But when I try to run a Get on it, it throws this error:
The LINQ expression 'DbSet()
.Where(s => s.IsUpcoming)' could not be translated. Additional information: Translation of member 'IsUpcoming' on entity type 'Show' failed. This commonly occurs when the specified member is unmapped. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information
Any tips on why/how would this work?
You have to add [NotMapped] attribute
[NotMapped]
public virtual bool IsUpcoming {
get
{
return Date >= DateTime.Now;
}
}
or Ignore if you use fluent api
modelBuilder.Entity<Show>().Ignore(c => c.IsUpComing);
but you will be able to use it after you get the DB set to your application( usually after ToList()). If you try to use it in a Linq query to get data from DB you will get the exception you have got already.
Using Breeze and a simple one to many relationship created using the Entity Data Model designer gives me the following error:
TypeError {stack: "TypeError: Cannot read property '$type' of null↵
…//localhost:55494/Scripts/jquery-1.9.1.js:1083:7)", query: null}
I figured out that Breeze throws that error when using a Navigational Property and that property is Null. What am I missing here?
I got the following relationship:
Controller code:
private readonly EFContextProvider<ModelContainer> _contextProvider = new EFContextProvider<ModelContainer>();
[HttpGet]
public string Metadata()
{
return _contextProvider.Metadata();
}
[HttpGet]
public IQueryable<Appointment> Appointments()
{
return _contextProvider.Context.Appointments;
}
Using the following query:
var query = breeze.EntityQuery.from('Appointments');
This is the raw JSON output:
[{"Id":1,"Date":"2013-01-01T00:00:00","Comments":"Testing","Car":null}]
Do you have a [BreezeController] attribute on your ApiController?
When I use database first, after creating the edmx file, all the conceptual models have already been generated. But I want to do some special operations on certain fields. For example, there's a field named 'price'; I want the matching property 'Price' to return double of the 'price'. How can I do that? If I modify the getter in the code, every time I update the model from database, all of the modifications go away.
What's the correct way to do this?
What you can do is create a partial class for entity which contains the Price Property and put a getter like this (A property with double price will be meaningful ),
Public partial class YourEntity{
Public float DoublePrice{
get { return Price*2;}
}
}
Or you can create a class inherited from the entity,
Public partial class Entity:YourEntity{
Public override float Price{
get { return base.Price*2;}
}
}
I am absolutely new to Entity Framework so please don't hesitate to point any errors. Anyway I'll try to describe my problem as I understand it.
I am creating a n-Tier application with Entity Framework. I am also using a generic repository as described in this page.
I was successful in populating a DetailsView and inserting a value to the database through an ObjectDataSource. However the problem I am encountering occurs when I am updating.
The error that I get is
An object with a key that matches the key of the supplied object could
not be found in the ObjectStateManager. Verify that the key values of
the supplied object match the key values of the object to which
changes must be applied.
And I understand WHY it happens as well. I use the following methods to populate the values from the DB.
public IEnumerable<TEntity> GetAll<TEntity>() where TEntity : class
{
return GetQuery<TEntity>().AsEnumerable();
}
public List<Company> GetAllCompanies()
{
return _genericRepository.GetAll<Company>().ToList();
}
If I put a breakpoint in the GetAll method and observe the results I see that anEntityKey is set and IsChangeTracked is true for each object in the list.
But if I check the object passed to the UpdateMethod specified for the ObjectDataSource, then EntityKey is null, EntityState is detached and IsChangeTracked is false. So because the EntityKey is null, I understand the exception is valid.
My update method looks like
public void Update<TEntity>(TEntity entity) where TEntity : class
{
ObjectSet<TEntity> objSet = DataContext.CreateObjectSet<TEntity>();
objSet.ApplyCurrentValues(entity);
SaveChanges();
}
I tried to attach using objSet.Attach(entity); then no exceptions but the object does not get updated either.
How do I properly perform the update using DetailsView ? Or how do I properly bind an ObjectDataSource to a DetailsView? How do I make sure that my EntityKey does not become null ?
Solved the issue using the method shown below
public void Update<TEntity>(TEntity entity) where TEntity : class
{
object originalItem;
EntityKey key = DataContext.CreateEntityKey(GetEntityName<TEntity>(), entity);
if (DataContext.TryGetObjectByKey(key, out originalItem))
{
DataContext.ApplyCurrentValues(key.EntitySetName, entity);
}
SaveChanges();
}
I have a stored procedure in my Entity Framework Model. I've added a Function Import and mapped the results to a Complex Type.
I want to add an extra property to this Complex type, that I'll populate in my Domain Service, not coming back from the stored procedure. I added a myClass.shared.cs file and implemented added the property like so:
//myClass.shared.cs
public partial class myClass
{
public string myProperty {get;set;}
}
I populate this in my domain service when I return the object, e.g.:
public myClass GetMyClass(int myClassID)
{
myClass theClass= this.ObjectContext.StoredProc(myClassID).FirstOrDefault();
class.myProperty = 12345;
return theClass;
}
When I get the return values of this method on the client side theClass.myProperty is always null but all values from the stored procedure are populated, am I missing something?
I've tried decorating the myProperty with the [DataMember] attribute but this throws the error:
"The type 'myClass' already contains a
definition for 'myProperty'"
How can I get this to return the value set in the Domain Service to the client?
There was no need to put this in the shared.cs class. The shared.cs class copies the actual code over to the client side and is useful for adding methods etc. but to add a new property, all I had to do was add a partial class (NOT in myClass.shared.cs) and decorate it with DataMember.
public partial class myClass
{
[DataMember]
public string myProperty {get;set;}
}