I followed a Tutorial a few weeks ago which shows how to create a simple ASP.NET MVC 3 App (http://www.asp.net/mvc/tutorials/mvc-music-store-part-1).
Now I created another app that is actually doing something very simple, but I cant make Visual Studio 2010 automatically creating a View that shows a selection of the 1:n connection.
The program is a simple News system with a relation NewsEntry.NewsCategory to NewsCategory.ID.
NewsEntry.cs
public class NewsEntry
{
public int ID { get; set; }
public string Title { get; set; }
public string ShortText { get; set; }
public string Text { get; set; }
public DateTime PublishDate { get; set; }
public DateTime UnpublishDate { get; set; }
public NewsCategory NewsCategory { get; set; }
}
NewsDB.cs
public class NewsCategory
{
public int ID { get; set; }
public string Name { get; set; }
public List<NewsEntry> News { get; set; }
}
NewsDB.cs
public class NewsDB : DbContext
{
public DbSet<NewsEntry> NewsEntry { get; set; }
public DbSet<NewsCategory> NewsCategory { get; set; }
}
So my question is what is missing that VS is not creating a view with category in a drop down list?
It obviously helped to add a NewsCategoryId field:
public class NewsEntry
{
public int NewsEntryId { get; set; }
public string Title { get; set; }
public string ShortText { get; set; }
public string Text { get; set; }
public DateTime PublishDate { get; set; }
public DateTime UnpublishDate { get; set; }
public int NewsCategoryId { get; set; }
public virtual NewsCategory NewsCategory { get; set; }
}
Related
I'm working with asp.net webforms and I'm a beginner with Entity Framework. I need to know how to make an update page and send data from page to a method (binding) without having to bind field by field and save changes.
Suppose I have this class :
public class Fiche
{
[Key]
public int Id { get; set; }
public string mrmme { get; set; }
public string nometprenom { get; set; }
public string adresse { get; set; }
public string ville { get; set; }
public string cp { get; set; }
public string tele { get; set; }
public string portable { get; set; }
public string datenaissance { get; set; }
public string email { get; set; }
public string situation { get; set; }
public string profession { get; set; }
#region // Santé & Prévoyance
public string typesp { get; set; }
public string nombresassures { get; set; }
public string regime { get; set; }
public string nbrenfants { get; set; }
public string mutuelleactuelle { get; set; }
public string tarifcontrat { get; set; }
public string niveaugaranties { get; set; }
public string mutuelle { get; set; }
#endregion
public string Createdbynom {
get {
return Data.Db.Users.Find(Createdby).nom;
}
}
public string Affectedtonom
{
get
{
return Data.Db.Users.Find(Affectedto).nom;
}
}
public ICollection<Contrat> Contrats { get; set; }
public ICollection<Commentaire> Commentaires { get; set; }
public string Createdby { get; set; }
[ForeignKey("Createdby")]
public ApplicationUser createuser { get; set; }
public string Affectedto { get; set; }
[ForeignKey("Affectedto")]
public ApplicationUser affetcteduser { get; set; }
}
I have lot of fields if I want to create a form to update this model. I need to create textbox for every field and when I click "submit" button, I need to get every value from every textbox and bind to model and call SaveChnages?
Is this the only way in webforms to update data a database?
Is there any way to binding data from my form to my update method?
I want to make the simple application to use with my PBX. I want to manage the providers and Lines tat connected to my PBX. The main objects are Provider and Line. Every provider has some lines connected to it. Lines can be added or deleted enabled or disabled and reserved. I want to keep the status of lines and history of actions in one table. Does this right solution or I need to create another table for history?
public class Line
{
public int LineId { get; set; }
public Provider Provider { get;set;}
public string Login { get; set; }
public string Pass { get; set; }
public string Domain { get; set; }
public string ProjectOwner { get; set; }
public string Project { get; set; }
public string OutNumber { get; set; }
public string InNumber { get; set; }
public string Description { get; set; }
public DateTime Begin_datetime { get; set; }
public DateTime End_datetime { get; set; }
public Human TechGuy { get; set; }
public Human Manager { get; set; }
}
public class Provider
{
public int ProviderId { get; set; }
public string Name { get; set; }
public string OurSideData { get; set; }
public string TheirSideData { get; set; }
public string Description { get; set; }
public DateTime Begin_datetime { get; set; }
public DateTime End_datetime { get; set; }
public Currency Currency { get; set; }
}
What is the best solution for the code first model creating in the logic like that? Maybe I need to add some additional fields?
You need another table for history. Something like:
public class Line
{
public int LineId { get; set; }
public virtual Provider Provider { get; set; }
public string Login { get; set; }
public string Pass { get; set; }
public string Domain { get; set; }
public string ProjectOwner { get; set; }
public string Project { get; set; }
public string OutNumber { get; set; }
public string InNumber { get; set; }
public string Description { get; set; }
public DateTime Begin_datetime { get; set; }
public DateTime End_datetime { get; set; }
public Human TechGuy { get; set; }
public Human Manager { get; set; }
public virtual ICollection<LineStatusHistory> ChangeHistory { get; } = new HashSet<LineStatusHistory>();
}
public class LineStatusHistory
{
[Key( )]
public int LineId { get; set; }
[Key()]
public int LineStatusHistoryId { get; set; }
public virtual Line Line { get; set; }
public DateTime ChangeDate { get; set; }
public string Change { get; set; }
}
public class Provider
{
public int ProviderId { get; set; }
public string Name { get; set; }
public string OurSideData { get; set; }
public string TheirSideData { get; set; }
public string Description { get; set; }
public DateTime Begin_datetime { get; set; }
public DateTime End_datetime { get; set; }
public Currency Currency { get; set; }
public virtual ICollection<Line> Lines { get; } = new HashSet<Line>();
}
I have a c# class as below
public class CreateStudent
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
}
and I need another class with following properties
public class EditStudent
{
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
public DateTime Date_of_Birth { get; set; }
}
I have repeated properties except that one field (Date_of_Birth) is added in the EditStudent Model.
Is there an option to reuse some of the properties from previous CreateStudent model
I am going to handle these data as Json objects in my front end angularjs based application
You could do this with a null-able property.
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
public DateTime? Date_of_Birth { get; set; }
}
This way you only have one Student model that can accommodate both use-cases.
you should be using inheritance feature here.
public class CreateStudent
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
}
public class EditStudent : CreateStudent
{
public DateTime Date_of_Birth { get; set; }
}
I need to extract data from a JSON string from a specific url (this).
When I convert it into C# by using jsontocsharp.com it looks like this:
public class Publisher
{
public int id { get; set; }
public string name { get; set; }
}
public class Title
{
public int id { get; set; }
public string name { get; set; }
public int subscribers { get; set; }
}
public class Issue
{
public List<object> alternates { get; set; }
public string complete_title { get; set; }
public string cover_image { get; set; }
public string description { get; set; }
public string diamond_id { get; set; }
public int id { get; set; }
public bool is_parent { get; set; }
public double? issue_number { get; set; }
public string other { get; set; }
public double price { get; set; }
public Publisher publisher { get; set; }
public string release_date { get; set; }
public Title title { get; set; }
}
public class RootObject
{
public int count { get; set; }
public string date { get; set; }
public List<Issue> issues { get; set; }
public int next { get; set; }
public object prev { get; set; }
public int total { get; set; }
}
Now I need to use these as my classes so I can gather and use data on further layers of my project. But I don't know how should I put the url in my code. I think this is a very basic question but I couldn't quite get the answer I'm looking for in the other questions. Any help greatly appreciated thank you.
Table 1: Articles
Table 2: ArticleCategories
how do I represent the relationship between the two tables which is a 1->1 relationship:
I can do the following, but I'm not sure it's the correct way :
public class Article
{
public int ArticleIndex { get; set; }
public int Category { get; set; }
public Guid User { get; set; }
public int Parent { get; set; }
public int Level { get; set; }
public int Order { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateExpires { get; set; }
public bool Show { get; set; }
public string Title { get; set; }
public string TitleHtml { get; set; }
public string Content { get; set; }
public string ContentHtml { get; set; }
public string ShortTitle { get; set; }
public ArticleCategory Category { get; set; }
}
public class ArticleCategory
{
public int CategoryIndex { get; set; }
public string Name { get; set; }
}
By convention, Code First expects an Id property for each class/table. Then you can do something like this:
public class Article
{
public int Id { get; set; }
public int ArticleIndex { get; set; }
public int Category { get; set; }
public Guid User { get; set; }
public int Parent { get; set; }
public int Level { get; set; }
public int Order { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateExpires { get; set; }
public bool Show { get; set; }
public string Title { get; set; }
public string TitleHtml { get; set; }
public string Content { get; set; }
public string ContentHtml { get; set; }
public string ShortTitle { get; set; }
public int ArticleCategoryId { get; set; }
public virtual ArticleCategory ArticleCategory { get; set; }
}
public class ArticleCategory
{
public int Id { get; set; }
public int CategoryIndex { get; set; }
public string Name { get; set; }
public virtual ICollection<Article> Articles { get; set; }
}
Note the virtual keyword. EF Code First needs this so it can perform its magic behind the scenes.
Now, if you are working with an Article, you can get all it's category info by doing article.ArticleCategory, and if you have an ArticleCategory you can find out what article it refers to with articleCategory.Articles.Single().
For more info, see this article by Scott Gu:
http://weblogs.asp.net/scottgu/archive/2010/12/08/announcing-entity-framework-code-first-ctp5-release.aspx