Does EF 4.2 Code First have support for enum types? If so, how do you use it? The following does not create a field for the enum property when using the Nuget EntityFramework package.
public class FooContext : DbContext
{
public IDbSet<Foo> Foos { get; set; }
}
public class Foo
{
public int Id { get; set; }
public string Name { get; set; }
public Category Category { get; set; }
}
public enum Category {One, Two, Three }
var db = new FooContext();
db.Foos.ToList();
EF team has changed versioning so EFv4.2 is not final release of June 2011 CTP. Features from June 2011 CTP should be released as part of .NET Framework 4.5.
No.
The enum support and many more features were included in the "June 2011 CTP" preview (see announcement) - but those require changes to the EF core and those will be shipped later.
EF 4.2 is only a few bugfixes and smaller changes - see ADO.NET team blog announcement.
What’s Not in This Release?
As covered earlier this release is just a small update to the
DbContext & Code First runtime. The features that were included in EF
June 2011 CTP require changes to the Core Entity Framework Libraries
that are part of the .NET Framework and will ship at a later date.
Related
I'm upgrading from EF Core 2.2 to EF Core 3.1. I have an entity Patient with a GUID Id:
class Patient {
public Guid Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
A DbSet is defined for this type:
public DbSet<Patient> Patients { get; set; }
In EF 2.2, when I added a new Patient, the Id would be automatically generated when the object was added to the DbSet:
// newPatient.Id -> 00000000-0000-0000-0000-000000000000
ctx.Patients.Add(newPatient);
// newPatient.Id -> C1D5ACB8-A4C9-4680-AF2F-BF5E5B0AC1B6 <=== GUID generated
Now, in EF 3.1, the Id is not being automatically generated upon Add:
// newPatient.Id -> 00000000-0000-0000-0000-000000000000
ctx.Patients.Add(newPatient);
// newPatient.Id -> 00000000-0000-0000-0000-000000000000 <=== still an empty GUID
This obviously breaks my code.
In EF Core 3.0 there is a breaking change "String and byte array keys are not client-generated by default". However, this breaking change indicates string and byte array keys, not GUID keys. Also, I have tried the recommended mitigations with FluentAPI and Data Annotations and it does not resolve my issue (still no GUID generated).
I am using the dotConnect for Oracle database provider v9.11. I did not find any breaking changes there that would affect this.
Of course I can explicitly assign a GUID Id in my code, but I'd like to make this work as it did before in EF Core 2.2.
Any suggestions why this is not working? Thanks.
I don't know if it's any different default configuration, but by creating an asp.net core 2.2 project and another asp.net core 3.0 project, I'm getting different results in model binding.
public class Dto
{
public string Prop1 { get; set; }
public string Prop2 { get; set; }
public string Prop3 { get; set; }
public string Prop4 { get; set; }
}
[HttpGet("test/{prop1:alpha}/{prop2:alpha}")]
public ActionResult<Result> Test(Dto dto)
{
}
The above code works perfectly in asp.net core 2.2 when the url is called:
https://localhost:xxxx/test/aaa/bbb/?prop3=ccc&prop4=ddd
However, in asp.net core 3 the object is null.
if i use [FromRoute] it just gets the values of prop1 and prop2.
If I use [FromQuery] it just gets the values of prop3 and prop4.
How do I configure asp.net core 3 so that it get the values of the route and querystring like asp.net core 2.2 ?
Note that in asp.net core 2.2 I have not informed either [FromRoute] or [FromQuery] that seem to me mandatory now.
OK, I found that the problem refers to using the ApiExplorer attribute.
Without it, it works exactly as I would like.
In time, it is possible to have the same results using ApiController attribute, however, by setting the SuppressInferBindingSourcesForParameters property to true.
Problem solved.
While researching whether or not ASP.NET MVC is suited for my next website, I've come across an annoying issue.
I have followed ASP.NET MVC since version 2, and it's gotten better. For instance, it's now fairly easy to get going with migrations in the entity framework with code first, which used to be a hassle.
This means that I now can get running with a database migrations and code first within half an hour (as I usually don't remember the steps involved, I have to follow a guide I wrote).
Now, fairly early on I always get a many-to-many relationship between entities (e.g. tags and posts) in my database, and what I've found is that getting this relationship exposed via MVC framework is surprisingly complicated! Example from asp.net Example from mikesdotnetting
It involves special methods to retrieve the relationship's data that is not an inherent part of the framework.
Is there really no better/easier way of treating the many-to-many relationship?
You should add a virtual key word to the Many port
public class Post
{
[Key]
public int ID { get; set; }
public string Title { get; set; }
public virtual ICollection<Tag> Tags {get;set;}
}
public class Tag
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Post> Posts {get;set;}
}
Currently I am building a web application with MVC 4 and Entity Framework code first scenario.
In the main structure of my application, I have a Dbcontext(BlogDB) to manage some Blog classes. It works fine as it created all the tables I need in the database. Then I created a Area to host an online store. My idea is to create a separated DbContext class(OnlineStoreDB) to handle the classes used for Online Store only.
My problem is once the OnlineStoreDB is fired, entity framework not only created tables for OnlineStore BUT ALSO removed old tables.
My questions are:
If you know a way to keep the old tables?
How exactly to manage the multi EF context classes in one application?
Code:
public class BlogDB : DbContext
{
public BlogDB ()
: base("DBConnection")
{
Database.SetInitializer(new BlogInitializer());
}
public DbSet<Blog> Blogs { get; set; }
public DbSet<Author> Authors { get; set; }
public DbSet<Comment> Comments { get; set; }
}
public class OnlineStoreDB : DbContext
{
public OnlineStoreDB() :
base("DbConnection")
{
Database.SetInitializer(new OnlineStoreInitializer());
}
public DbSet<Order> Orders { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<User> Users { get; set; }
}
Xavier, welcome to code-first!
Yes, code-first was a brilliant approach that promised soooo much. But now you've hit the catch. There's no clever mind at Microsoft (or outside as far as I know) who has come up with a smooth way to alter tables intelligently without endangering the data and possibly schema.
2 Years ago, the implementation strategy was to drop and re-build the DB. That was just intolerable as many of us didn't have SU access and were stopped in our tracks.
For all the advantages I found from code first, I prefer DB first. While data can't be preserved easily, annotations can through buddy classes.
Microsoft has come up with some clever Migration Strategies. I strongly suggest you read both articles. Code Project 2nd:
1) http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx
2) http://www.codeproject.com/Articles/504720/EntityplusFrameworkplusCodeplusFirstplusMigrations
whether you decide to continue with Code-First, they should be enlightening. I sound like a critic but I'm torn between advantages of one and stability of the other.
Finally, I don't think you should preserve 2 dbcontexts. Your POCOs should be consolidated under 1 context.
If you want to keep the tables not changed you need to set initializer to null in both DBContexts if they have sub set of tables .
But I don't see a point that you create two DBContexts for one database. Can you clearly separate two set of tables(Domains) in you database?
I am trying to create a simple EF code first sample using SQL server compact edition 4.0.
Technologies used include: Visual Studio 2012 Ultimate.
So i created a simple poco class:
namespace MvcApplication2.Models
{
public class Customer
{
public int CustomerId { get; set; }
public string CustomerName { get; set; }
}
}
And a simple context class:
namespace MvcApplication2.Models
{
public class CustomerEntity : DbContext
{
public DbSet<Customer> Customers { get; set; }
}
}
Then I tried to create a controller class with the template: MVC Controller with read/write actions and views using entity framework.
Model class -> Customer
Data context class -> CustomerEntity
I got the following error:
Unable to retreive metadata for ... Using the same dbcompiledmodel to
create contexts against different types of database servers is not
supported. ...
I should say this exact same code worked without problem using LocalDB.
The only difference that I thought was needed, was the addition of the following to the web.config file.
<add name="CustomerEntity"
connectionString="Data Source=|DataDirectory|CustomerEntity.sdf"
providerName="System.Data.SqlServerCe.4.0" />
Am I missing something else?
I know I can't be the only one having this problem.
Your problem in dublicate DB in EF cache and DB in your App_Data. The easiest way to resolve this problem is to comment your default constructor CustomerEntity, to create controller from template and then uncomment it.