I have a model like the following:
[Table("forms", Schema = "mySchema")]
public class forms
{
[Key]
public int ID { get; set; }
public string field1 { get; set; }
public string field2 { get; set; }
}
This works in MVC 3, however in MVC 4 the attribute Table in not recognized. I have the System.ComponentModel.DataAnnotations namespace included, and the dll referenced, along with EntityFramework.dll. The version of EF has changed between MVC 3 and 4. If I reference the MVC 3 EF dll, Table is recognized, however Schema is not. The reason for using the table attribute is so I can specify the schema. What am I missing?
According to msdn the TableAttribute supports the schema property.
Maybe use:
[Table(Name = "forms", Schema = "mySchema")]
Related
I'm working on an ASP.NET MVC 5 project in which I'm trying to build a controller from a MVC View Model. That ViewModel brings together 6 tables which I need to show in the views. Its my understanding that using MVC ViewModels is one way of showing multiple tables in a view. Anyway, I'm getting the following error message:
Error
There was an error running the selected code generator: 'Unable to retrieve metadata for
'PrismSmallTasks.ViewModels.ManageInterviewVM'. One of more validation errors were
detected during model generation:
ManageInterviewVM:: EntityType 'ManageInterviewVM' has no key defined.
Define the key for this EntityType.
ManageInterviewVMs: EntityType: EntitySet 'ManageInterviewsVMs' is based on
type 'ManageInterviewVM' that has no keys defined.
There's no key in the ManageInterviewVM ViewModel. That's because it's comprised of lists of the tables represented in the models that are in the VM. And each of those model classes do have a column that has a key defined for it.
For example, here's ManageInterviewVM:
public class ManageInterviewVM
{
public List<FieldRecord> FieldRecords { get; set; }
public List<TaskList> TaskLists { get; set; }
public List<InterviewARVTreatment> InterviewARVTreatments { get; set; }
public List<Note> Notes { get; set; }
public List<Risk> Risks { get; set; }
public List<Interview1> Interviews { get; set; }
}
And here's a partial listing of one of those tables as it is defined in the model class:
public partial class TaskList
{
[Key]
public int ID_TaskList { get; set; }
[Required]
[StringLength(15)]
public string CD_TaskListType { get; set; }
public int? ID_Profile { get; set; }
public int? ID_FieldRecord { get; set; }
public int? ID_Interview { get; set; }
So, I don't know what I'm missing. Why is this error showing up and how I can resolve it?
Your ViewModels should be completely dissociated from your Data Context (Data Access Layer). Only your domain models should deal with the DAL. ViewModels are only for displaying specific information to the view.
So after you create your ViewModel.. you try and create your view. When you arrive at this screen:
Type in your view name
Pick your template (if you leave it as 'Empty (without model)' then you should be able to just create it without any issue).
Once you pick a specific template and Model class (ViewModel), the 'Data context class' will auto-populate with your connection string (dbcontext), which is where your problem lies.
Since viewmodels are not supposed to be associated with the data access layer, you can just delete what is auto-populated in the 'Data context class' and then you should be able to create your view.
If you fall into the trap of thinking that you need to define keys for your viewmodel.. then your viewmodel class will be added to your connection string's class (dbcontext class).. which is a no-no.
You need to query the database using your domain models.. then you assign those values to your ViewModels properties that you want to display.
Hope this helps!
In my MVC4 application I had the following model:
public partial class UserProfile
{
public UserProfile()
{
this.webpages_Roles = new List<webpages_Roles>();
}
public int UserId { get; set; }
public string UserName { get; set; }
public virtual ICollection<webpages_Roles> webpages_Roles { get; set; }
}
I used a select with Entity Framework against this class / table to get a list of UserId's and UserNames.
Now I understand it's completely different with MVC5.
Can someone tell me how I can get the same information with MVC5? Is there a built in method that I can use?
You can enumerate all of the users in the 1.0 RTM Identity bits via the EF specific IdentityDbContext.Users property.
In a future release(probably 1.1), we will be adding an IQueryable for TUser to the UserManager as well, so you don't have to drop down to the specific store to enumerate the users.
We have a model class defined that I want to produce from our EF 4.0 edmx for persistence. The class looks roughly as follows:
[DataContract]
public class Schedule
{
[DataMember]
public string Name { get; set; }
[DataMember]
public Guid Id { get; set; }
[DataMember]
public DateTime RunDate { get; set; }
[DataMember]
public IList<Guid> Routes { get; set; }
[DataMember]
public IList<Guid> Paths { get; set; }
}
How do I represent Routes and Paths on the edmx design surface? I can't see anyway of doing this other than creating two entities with a single Guid Id field then setting a 1-* Association to Schedule. I'd rather not have to do that as we'll then have a Route and Path class that isn't what we want at the moment.
We haven't had chance to look at Code First yet and don't really have time to figure it out for this project but would it support our needs?
Thanks for any assistance.
You must either use related entities or you musn't map them directly. You can for example map another fields called RoutesSerialized and PathsSerialized which will be of type string and contains all Guids stored as strings and separated by semicolon. Your current properties will use return IEnumerable and use internally use functions like String.Join, String.Split, ToString and Guid.Parse.
I have a model as follows:
namespace MvcApplication1.Models
{
public enum Sex { Male, Female }
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
[Required(ErrorMessage = "Please select either Female or Male.")]
public Sex? Sex { get; set; }
}
}
I successfully generated a database using EFCodeFirst. Unfortunately, only Id and Name columns are created in the database and no Sex column is generated.
How to make EFCodeFirst generate a column of type int for a property of type enum?
As of EF CTP5, enums are still NOT supported. This is something that the EF team are working on though and we will likely get it on the RTM. For now you would have to use an int? for the Sex property.
EF Code First will ignore any property that is not a primitive type when creating a DB from your model.
There is a simple workaround you can use:
http://daniel.wertheim.se/2010/06/09/dealing-with-enumerations-and-entity-framework-4-code-first/
..but you might have to do a little refactoring once EF supports enums.
I set values for the Order property of the Display attribute in my model metadata.
[MetadataType(typeof(OccasionMetadata))]
public partial class Occasion
{
private class OccasionMetadata
{
[ScaffoldColumn(false)]
public object Id { get; set; }
[Required]
[Display(Name = "Title", Order = 0)]
public object Designation { get; set; }
[Required]
[DataType(DataType.MultilineText)]
[Display(Order = 3)]
public object Summary { get; set; }
[Required]
[DataType(DataType.DateTime)]
[Display(Order = 1)]
public object Start { get; set; }
[Required]
[DataType(DataType.DateTime)]
[Display(Order = 2)]
public object Finish { get; set; }
}
}
I present my models in strongly-typed views using the DisplayForModel and EditorForModel methods.
<%= Html.DisplayForModel() %>
and
<%= Html.EditorForModel() %>
But, ASP.NET MVC 2 displays the fields out of order! What might I have wrong?
.NET 4 DataAnnotations comes with a
new Display attribute that has
several properties including
specifying the value that is used for
display in the UI and a ResourceType.
Unfortunately, this attribute is new
and is not supported in MVC 2 RTM.
The good news is it will be supported
and is currently available in the MVC
Futures release.
The steps to get this working are
shown below...
from Localization in ASP.NET MVC 2 using ModelMetadata by Raj Kaimal
Brad Wilson said November 2009:
There is no support for order in MVC
2, and it's not likely to be there
until MVC 3. One major reason is that
DataAnnotations in .NET 4 have added
ordering support, but since we rely on
3.5, we cannot do it yet.
from comment on "ASP.NET MVC 2 Templates, Part 5: Master Page Templates"