Entity Framework 6 where did key and column attributes go - c#

I'm upgrading from Entity Framework 4.1.0.0 to Entity Framework 6 and I'm having a really hard time tracking where all of the old classes went and what namespace things are in these days.
I've got a class that looks like this and was using these attributes but now I
[Key, Column(Order = 1)]
public int PersonID { get; set; }
[Key, Column(Order = 2)]
public int WorkoutID { get; set; }
etc...
Can't figure out what namespace the attributes are in
Even figure out if I'm supposed to be using them
Can't nuke the EF class lib and rebuild it because the DB is code-first - I think..
Disclaimer: I've never really worked from code-first because I like being able to rebuild my edmx file from scratch
Can anyone point me in the right direction? MSDN docs have some up short, the EF 6 upgrade guide (found here: http://visualstudiomagazine.com/articles/2014/03/01/whats-new-in-entity-framework-6.aspx) doesn't contain what I need and you'll all my last hope (not really - I'll keep looking no matter what)

I am not sure that I understand your question, but all of EF6 CF attributes are in :
System.ComponentModel.DataAnnotations
you can find more here:
System.ComponentModel.DataAnnotations Namespace ()

The attributes dealing with database schema moved to: System.ComponentModel.DataAnnotations.Schema Namespace.
That's why they may appear missing when you upgraded, but just adding that namespace to your code file will get things back working.

Related

Entity Framework 6.2 Code First errors

I have some issues with Entity framework 6.2. I change ef version and now I have a lot bug..
EF version: 6.2
Visual studio version: 15.5.2
.Net version: 4.7.1
OS: Windows 10 Pro 1709
1.NotMapped why not working any more with inheritance? My example class:
public class BaseClass {
public string MappedProp {get;set;}
public virtual string NotBeMappedProp {get;set;}
}
public class Test : BaseClass {
public string MappedProp {get;set;}
[NotMapped]
public override string NotBeMappedProp {get;set;}
}
add-migration not found entity framework on project. But I installed it already. Besides, I deleted all packages folder. However still continue same exception.
I open clean project but suprise... I have a new proplem. My foreign keys thrown an exception.
Unable to determine the principal end of an association between the
types x1 and x2. The principal end of this association must be
explicitly configured using either the relationship fluent API or data
annotations.
My code part looking like that:
public class Student{
.....
public string Name {get;set;}
public long? LocationId {get;set;}
[ForeingKey("LocationId")]
public Location Address {get;set;}
......
}
public class Location{
public long Id {get;set;}
........
}
It is working with previous version.
I have no migration, I updated my database, check table but entity framework still said, there is an migration.
the model backing the context has changed since the database was
created
Try add abstract modifier to BaseClass definition. NotMapped attribute should be at the lowest level. If you need to map overridden property you should map it with Column attribute directly in inherited class.
Try run command Install-Package EntityFramework -Version 6.2.0 -Project {{EFProjectName}} to reinstall package and reference it correctly.
You better want to specify ForeignKey attribute in Address class and its StudentId property(or whatever you call it). It is one-to-zero-or-one relationship.
Information about migrations are stored in the database table __MigrationHistory along with compiled db model to speed things up(checking everytime if code suits database is time consuming) and that is the reason you get that error. You have different compiled model in your code and different stored in the migration history. You can create empty migration running command Add-Migration -Name ManualDbUpdate -IgnoreChanges to overcomes this, but you must be sure code model and database model are equal. If not you are going to get exceptions.

Unable to Add Controller

I am new to MVC web application development.
I am trying to add a controller after adding my model and DbContext class.
But when i am trying to this controller using Entity framework it gives me an error of
Unable to cast object of type 'System.Data.Entity.Core.Objects.ObjectContext' to 'System.Data.Objects.ObjectContext'
I am using EF-6.1.1 (latest update)
Following are my Model and Context Class..
public class EmpDetails
{
[Key]
public int Id { get; set; }
public string EmpId { get; set; }
public string EmployeeName { get; set; }
}
public class ModelContext : DbContext
{
public DbSet<EmpDetails> Employee { get; set; }
}
When i am trying to add a controller I get following error.
Please suggest some solution to this problem. what is going wrong with it..
here is the process through which i am adding Controller
Entity Framework brought breaking changes between versions 5 and 6. In order for it to go completely open source, they moved all of the libraries out of band and they are now all completely within the EntityFramework assembly in NuGet. A side effect of this was that many of the namespaces for Entity Framework has changed:
The namespaces for DbContext and Code First types have not changed.
This means for many applications that use EF 4.1 or later you will not
need to change anything.
Types like ObjectContext that were previously in
System.Data.Entity.dll have been moved to new namespaces. This means
you may need to update your using or Import directives to build
against EF6.
The general rule for namespace changes is that any type in
System.Data.* is moved to System.Data.Entity.Core.*. In other words,
just insert Entity.Core. after System.Data. For example:
System.Data.EntityException => System.Data.Entity.Core.EntityException
System.Data.Objects.ObjectContext =>
System.Data.Entity.Core.Objects.ObjectContext
System.Data.Objects.DataClasses.RelationshipManager =>
System.Data.Entity.Core.Objects.DataClasses.RelationshipManager
The reason you are seeing the error is that you are using a previous version of MVC, which was targeting an earlier version of Entity Framework. The scaffolding is going to be assuming the old namespaces.
You can try upgrading to the newest version of MVC and your scaffolding will work again. Either that or downgrade EF6 (I don't recommend this, it has a lot of really great features). The third option is to manually fix your scaffolded code every time.
While using ASP.Net MVC 3/4, Entity framework assembly (.dll) would be automatically referenced with a lower version (5.0.0.0). And when you update this to a higher version an explicit type conversion is required for which you are getting this error. one way to fix this problem is, use the existing version of Entity Framework (5.0.0.0) without updating to higher version.

custom code generation strategy for friendly navigation property

I'm doing a DB-First generation of a DbContext and POCO objects with EF6 and having the same issues as many others re the navigation property names being unhelpful in describing relationships to other tables.
e.g If a Person has a Home and Work address, it would be reflected in the object as
public class Person {
public virtual DbSet<Address> Address;
public virtual DbSet<Address> Address1;
}
This question is on the right track for EF5, however neither the tool nor the code solution supports the t4 template generated by EF6.
Improve navigation property names when reverse engineering a database
What's the easiest way I can replace the above with
public class Person {
public virtual DbSet<Address> Home;
public virtual DbSet<Address> Work;
}
AND be able to regenerate the edmx file when I need to from scratch (i.e manually modifying 100 tables via the vs2013 GUI is not what I'm looking for).
I've looked around the forums and have started using the debug T4 Template tool but hoping there is a easier out than DIY.
If you don't need edmx, then you can remove it at all and reverse your database into Code First using EF Power Tools. After that use any tool that will help with property renames e.g Resharper

Specify decimal scale as an attribute using EF code first

I'm using Entity Framework Code First to query my SQL Server database. I'm also using code migrations to keep it up to date.
When I use the decimal type, the underlying columns are created with a precision/scale of 18,2. For one particular column (exchange rate) I would like to increase this to 18,6.
I'd like to add an attribute to my class that will instruct EF to use the specified decimal precision, as in the following example :
public class Transaction
{
[Key]
public int ID { get; set; }
...
[DisplayName("Exchange Rate")]
[DecimalPrecision(18, 6)]
public decimal? ExchangeRate { get; set; }
}
What I do not want to do is update the OnModelCreating function as suggesting in this question
Over here I found a potential solution, but I then discovered that code won't compile as it is based on pre-release functionality that was then removed.
An alternative is proposed here, but that looks way complicated, and perhaps before I wade in I thought I might ask if an alternative exists? EF is moving so fast it's hard to keep up with the evolutions, perhaps I missed something.

EF4 exception with relationship

I have two entities and there are their POCO:
public class DocumentColumn
{
public virtual long Id { get; set; }
public virtual string Name { get; set; }
public virtual long? DocumentTypeId { get; set; }
}
public class DocumentType {
public virtual long Id { get; set; }
public virtual string Name { get; set; }
}
There is a relation between those two entities. In the db the relation called:FK_T_DOCUMENT_COLUMN_T_DOCUMENT_TYPE.
When I do:
DocumentColumns.Where(x => x.DocumentTypeId == documentTypeId).ToList();
I get the exception:
{"Metadata information for the relationship 'MyModel.FK_T_DOCUMENT_COLUMN_T_DOCUMENT_TYPE' could not be retrieved. If mapping attributes are used, make sure that the EdmRelationshipAttribute for the relationship has been defined in the assembly. When using convention-based mapping, metadata information for relationships between detached entities cannot be determined.\r\nParameter name: relationshipName"}
I tryed to remove the relationship and the DocumentColumn table and reload them but the code still throws the exception.
Whet does this exception means and how can I solve it?
EDIT:
The exception happens also If I do DocumentColumns.ToList();
(Presuming you are talking about Code First ....)
There is no information in either class to let CF know that there is a relationship between them. It doesn't matter that the database has the info. Entity Framework needs to have a clue about the relationship. You provide only a property with an integer. CF cannot infer a relationship. You must have something in one class or another that provides type or another. This is not a database. It's a data model. Very different things.
But that's not all. I'm guessing that this is a one to many relationship. You could either put a List property into the Document class or a Document property in the DocumentColumn class. If you only do the latter, CF and EF will NOT know about the 1:. It will presume a 1:1 (that is if you leave DocumentId integer in there, otherwise it will presume a 1:0..1). However, I think you could get away with this and then just configure the multiplicity (1:) in fluent API.
UPDATE...reading your question again, I think you are using an EDMX and designer not code first. What are you using to create your POCO classes? Are you doing code gen from the EDMX or just writing the classes. I still think the lack of a navigation property in at least ONE of the types might be the cause of the problem. The ERROR message does not suggest that...I'm only coming to this conclusion by looking at the classes and inferring my understanding of how EF works with the metadata. I could be barking up the wrong tree. FWIW, I have asked the team if they are familiar with this exception and can provide some idea of what pattern would create it. It's pretty bizarre. :)
It seems odd to me that you are using EF with a defined relationship and you are not using the related property. Can you not do:
DocumentColumns.Where(x=>x.DocumentType.Id == documentTypeId).ToList();
This is what I would expect to see in this instance.

Categories

Resources