I created an mvc 4 web project using database first and entity framework but was surprised to see that default models were not generated automatically, is there a way to create them from my .edmx file or are there any other possibilities to save hand coding them?
Any help would be much appreciated
UPDATE:
Sorry, should have mentioned I am looking for a solution to generate simple models with basic methods such as :
public class EditUserModel
{
public int ID { get; set; }
[Required(ErrorMessage = "Forename is required")]
public string Forename { get; set; }
[Required(ErrorMessage = "Surname is required")]
public string Surname { get; set; }
[Required(ErrorMessage = "Username is required")]
public string Username { get; set; }
}
I ended up making a SQL query to generate a simple mvc model from my tables.
Related
I've hit a snag while building a .net mvc site. I have 2 related objects and am struggling with properly linking them. Specifically:
public class Address
{
public int AddressId { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostCode { get; set; }
[ForeignKey("AddressCategory")] // <-- EF adds field to below object's table
public int AddressCategoryId { get; set; }
public virtual AddressCategory AddressCategory { get; set; }
}
public class AddressCategory
{
public int AddressCategoryId { get; set; }
public string Description { get; set; }
}
Adding the [ForeignKey] data annotation to the Address object results in EF adding an Address_AddressId column to the AddressCategory table, which I don't want (or need) to happen.
I've tried to omit the ForeignKey attribute, but then I run into other errors because .net can't link the tables (e.g. Unknown column 'Extent1.AddressId' in 'field list'). Additionally, I wouldn't be able to use:
var addresses = db.Addresses.Include(l => l.AddressCategory);
Is there any way to link the 2 tables without EF adding an additional column to the AddressCategory table?
Thank you to #cloudikka for responding. After much trial-and-error I seem to have gotten it to work simply by omitting any ForeignKey reference from either object. I let EF rebuild the database and perform all scaffolding (CRUD forms) and they have been created perfectly.
My take-away is that foreign key attributes should be used for parent-child relationships, but not for look-up tables. I clearly have much to learn about asp.net mvc!
I am learning ASP.NET MVC. And In order to create PK and FK I have added some code in models in .cs file as below
public class Courses
{
public int CourseID { get; set; }
[Key]
public string CourseName { get; set; }
public string Description { get; set; }
public int Duration { get; set; }
public virtual ICollection<Instructors> Instructors { get; set; }
}
public class Instructors
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string CourseName { get; set; }
[ForeignKey("CourseName")]
public virtual Courses Courses { get; set; }
}
My question is then what is the significance of Fluent API. I have successfully created PK and FK using above code in model. Then what is Fluent API and why it is needed?
Entity framework Fluent Api is an alternative way to define database schema using Entity framework Code First approach. The syntax that you used in your question uses data annotations which is other common approach.
The major advantage of FluentApi is that you don't have to have actual access to your model classes in order to decorate them. For example when you have your models in separate assembly decorating them with annotations is simply impossible. On the other hand by using EF FluentAPI you can easily do it with something like:
modelBuilder.Entity<Courses>().HasKey(c => c.CourseID);
At the bottom line both approaches generate exactly the same database schema.
So if you prefer to use data annotations and your project structure allows it, you can use it without any doubts. In those cases when you can not use the data annotations approach and you still want to use Code First approach, FluentApi is the right way to look at.
I generated Entity Model from my database in my MVC5 App.
When I try to add [DispalyName] to some properties it works fine, but after some time app refreshes this class by itself and removes all my custom code
public partial class Patient
{
public Patient()
{
this.PatientDetails = new HashSet<PatientDetail>();
}
public int PatientID { get; set; }
[DisplayName("Last Name")]
public string LastName { get; set; }
public string FirstName { get; set; }
public virtual ICollection<PatientDetail> PatientDetails { get; set; }
}
Why MVC does it and how to disable that?
I believe since you're using Database first, the entities will get completely re-created every time you refresh, thus you lose your custom attributes.
Also, to go off of Joe's comment, you should make a view model and put your [Display] attributes there and not directly on the entity.
I have been Googleing this and had no joy.
How do i add unique to a field in a code first approach in mvc4 .net
public class Religion
{
public int ReligionId { get; set; }
[Required]
[StringLength(200)]
public string Description { get; set; }
}
What would i need to add to description for this to work
you may use remote validation
review that link http://msdn.microsoft.com/en-us/library/gg508808(v=vs.98).aspx
simple and fast
I have been working with EF5 trying to build an application and have run into a small problem.
I have created a model like
public class TargetBusinessModel
{
public int Id { get; set; }
public Guid BusinessId {get; set; }
public Business Business { get; set; }
public string ContactName { get; set; }
public string ContactTitle { get; set; }
public string ContactPhone { get; set; }
}
Updated the Context file
public DbSet<TargetBusinessModel> TargetBusinessModels { get; set; }
My problem is none of the properties from Business are mapped within the database.
The Business Model I am trying to add is from another project, I am not sure if that's the reason.
I don't mind if the code first creates a separate table for my Business model or combines them together.
Can anyone help out?
Try to add DbSet for Business entities to your DbContext implementation:
public DbSet<Business> Businesses { get; set; }