I am making this a school project and I am first time using the Entity Framework so don't be too harsh about it. These are my tables/classes.
public class Database : DbContext
{
public Database() {}
public DbSet<Producer> Producers { get; set; }
public DbSet<Song> Songs { get; set; }
public DbSet<ProducerSong> ProducerGlasbos { get; set; }
}
public class Song
{
public int Id { get; set; }
public string Title { get; set; }
public int Length { get; set; }
public int Views { get; set; }
}
public class Producer
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }
public string name { get; set; }
public string surname { get; set; }
public int birth { get; set; }
}
public class ProducerSong
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }
public Producer Producer { get; set; }
public Song Songs { get; set; }
public int year { get; set; }
}
When I try to inset new instance of a class into table/database in just crashes and throws error
Data.Glasbas.Add(new Glasba { length = 200, Naslov = "Bolecina", views= 313});
Error screenshot: Look at me
Thanks for helping.
public class Database : DbContext
{
public Database() { }
public DbSet<Producer> Producers { get; set; }
public DbSet<Song> Songs { get; set; }
public DbSet<ProducerSong> ProducerGlasbos { get; set; }
public class ProducerSong
{
[System.ComponentModel.DataAnnotations.Key]
public int ProducerSongID { get; set; }
public Producer Producer { get; set; }
public Song Songs { get; set; }
public int year { get; set; }
}
public class Song
{
[System.ComponentModel.DataAnnotations.Key]
public int Id { get; set; }
public string Title { get; set; }
public int lenght { get; set; }
public int views { get; set; }
//ForeignKey
public int ProducerSongID { get; set; }
public ProducerSong ProducerSong { get; set; }
}
public class Producer
{
[System.ComponentModel.DataAnnotations.Key]
public int Id { get; set; }
public string name { get; set; }
public string surname { get; set; }
public int birth { get; set; }
//ForeignKey
public int ProducerSongID { get; set; }
public ProducerSong ProducerSong { get; set; }
}
}
The problem was in constructors if I used constructors to create objects it just don't work odd but what can you do..
Related
I know [FromBody] allows only one complex type as a parameter. I just wanted to know, is there any other way to do so? Any help or knowledge would be greatly appreciated! Thanks! :)
This is my controller
public HttpResponseMessage Post([FromBody] List<TranInOutDtl> tranInOutDtl, List<TranInOutDtlsub> tranDtlSub, List<TranInOutDtlsub> tranDtlSub)
This is my complex object below
public partial class TranInOutDtl
{
public int Tranno { get; set; }
public int Trannosub { get; set; }
public string ProdTypeDesc { get; set; }
public string BatchNo { get; set; }
public string Itemno { get; set; }
public string ItemDesc { get; set; }
public decimal ScanPendQty { get; set; }
public int TotalBoxqty { get; set; }
public decimal Quantity { get; set; }
public int BoxQty { get; set; }
public bool Isdone { get; set; }
public int PKId { get; set; }
public int PKSubId { get; set; }
public string PkBxDesc { get; set; }
public int BoxSz { get; set; }
}
public partial class TranInOutDtlsub
{
public int Tranno { get; set; }
public int Trannosub { get; set; }
public int Trannosub1 { get; set; }
public string RackShlvNo { get; set; }
public string ShlvNo { get; set; }
public int ShlvBoxQty { get; set; }
public decimal Quantity { get; set; }
public int BoxQty { get; set; }
public bool Isdonesub { get; set; }
public string RkShlvSelType { get; set; }
public string RkShCatUId { get; set; }
public string RackCatDesc { get; set; }
public string RkShCatColorCode { get; set; }
}
public partial class TranInOutRackScan
{
public int Tranno { get; set; }
public int Trannosub { get; set; }
public int Trannosub1 { get; set; }
public int Srno { get; set; }
public string BarcodeNo { get; set; }
public decimal Quantity { get; set; }
public int BoxQty { get; set; }
public string InOut { get; set; }
public int PackMaster_ID { get; set; }
public int Pack_type_ID { get; set; }
}
As it was mentioned by DavidG you could create any complex types that will suit your specific needs so for example
public class TranInOutContainer
{
public List<TranInOutDtl> TranInOutDtl Dtl {get; set;}
public List<TranInOutDtlsub> TranDtlSub DtlSub {get; set;}
....
}
will be valid solution for your problem
You could also use dynamic type ofc but it should be used only if no other solution exists
public class LootItem
{
public int Id { get; set; }
public string Name { get; set; }
public string Ilvl { get; set; }
public ItemSlot slot { get; set; }
public virtual ICollection<LootPlayer> Player { get; set; }
public virtual ICollection<LootPlayer> PlayerBis { get; set; }
public LootItem()
{
Player = new List<LootPlayer>();
PlayerBis = new List<LootPlayer>();
}
}
public class LootPlayer
{
public int Id { get; set; }
public string Name { get; set; }
public LootClass PlayerClass { get; set; }
public LootRole PlayerRole { get; set; }
public virtual ICollection<LootItem> CurrentGear { get; set; }
public virtual ICollection<LootItem> BisGear { get; set; }
public LootPlayer()
{
CurrentGear = new List<LootItem>();
BisGear = new List<LootItem>();
}
}
I'm trying to create 2 x many to many relations between these 2 tables.
Player <-> CurrentGear
PlayerBis <-> BisGear
Is this posible, because currently when I run the Update-Database I don't get any relationship tables. Only LootItem and LootPlayer
You Can't create many-to-many relationship. you should create two one-to-many relationship like this. create an class like this
public class LootPlayerLootItem
{
public int Id { get; set; }
public int PlayerId { get; set; }
public int PlayerBisId { get; set; }
public int CurrentGearId { get; set; }
public int BisGearId { get; set; }
[ForeignKey("CurrentGearId")]
public LootPlayer CurrentGear { get; set; }
[ForeignKey("BisGearId")]
public LootPlayer BisGear { get; set; }
[ForeignKey("PlayerId")]
public LootItem Player { get; set; }
[ForeignKey("PlayerBisId")]
public LootItem PlayerBis { get; set; }
}
then change the LootItem and LootPlayer to this
public class LootItem
{
public int Id { get; set; }
public string Name { get; set; }
public string Ilvl { get; set; }
public ItemSlot slot { get; set; }
[InverseProperty("Player")]
public virtual ICollection<LootPlayerLootItem> Player { get; set; }
[InverseProperty("PlayerBis")]
public virtual ICollection<LootPlayerLootItem> PlayerBis { get; set; }
public LootItem()
{
Player = new List<LootPlayerLootItem>();
PlayerBis = new List<LootPlayerLootItem>();
}
}
public class LootPlayer
{
public int Id { get; set; }
public string Name { get; set; }
public LootClass PlayerClass { get; set; }
public LootRole PlayerRole { get; set; }
[InverseProperty("CurrentGear")]
public virtual ICollection<LootPlayerLootItem> CurrentGear { get; set; }
[InverseProperty("BisGear")]
public virtual ICollection<LootPlayerLootItem> BisGear { get; set; }
public LootPlayer()
{
CurrentGear = new List<LootPlayerLootItem>();
BisGear = new List<LootPlayerLootItem>();
}
}
I have the following class object structure:
public class StatusObj3
{
public int status { get; set; }
public DataObj3 data { get; set; }
}
public class DataObj3
{
public string survey_id { get; set; }
public string date_created { get; set; }
public string date_modified { get; set; }
public int custom_variable_count { get; set; }
public List<Custom_VariablesObj> custom_variables { get; set; }
public int language_id { get; set; }
public int num_responses { get; set; }
public int question_count { get; set; }
public string nickname { get; set; }
public TitleObj title { get; set; }
public List<PagesObj> pages { get; set; }
}
public class Custom_VariablesObj
{
public string variable_label { get; set; }
public string question_id { get; set; }
}
public class TitleObj
{
public bool enabled { get; set; }
public string text { get; set; }
}
public class PagesObj
{
string page_id { get; set; }
string heading { get; set; }
string sub_heading { get; set; }
public List<QuestionObj> questions { get; set; }
}
public class QuestionObj
{
public string question_id { get; set; }
public string heading { get; set; }
public string position { get; set; }
public QuestionTypeObj type { get; set; }
public List<AnswerObj> answers { get; set; }
}
public class QuestionTypeObj
{
public string family { get; set; }
public string subtype { get; set; }
}
public class AnswerObj
{
public string answer_id { get; set; }
public int position { get; set; }
public string text { get; set; }
public string type { get; set; }
public bool visible { get; set; }
public int weight { get; set; }
public bool apply_all_rows { get; set; }
public bool is_answer { get; set; }
public List<ItemsObj> items { get; set; }
}
public class ItemsObj
{
public string answer_id { get; set; }
public int position { get; set; }
public string type { get; set; }
public string text { get; set; }
}
I 've deserialized json into this object via:
var results_SurveyDetails = deserializer.Deserialize<StatusObj3>(json_returned);
I'm trying to loop thru the pages by:
foreach (var page in results_SurveyDetails.data.pages)
However, not all members of page are available to use.
Only the page.questions is there and not page_id, heading and subheading.
What am I doing wrong?
You are missing public on your other properties in class PagesObj.
I have a class with nested subclasses:
public class listDevicesModel
{
public int totalCount { get; set; }
public Messages messages { get; set; }
public Devices devices { get; set; }
public class Entry
{
public string key { get; set; }
public object value { get; set; }
}
public class Detail
{
public List<Entry> entry { get; set; }
}
public class Devices
{
public List<Device> device { get; set; }
}
public class Device
{
public string #id { get; set; }
public string uuid { get; set; }
public string principal { get; set; }
public int blockReason { get; set; }
public int clientId { get; set; }
public string comment { get; set; }
public int compliance { get; set; }
public int countryCode { get; set; }
public int countryId { get; set; }
public string countryName { get; set; }
public string createdAt { get; set; }
public string currentPhoneNumber { get; set; }
public List<Detail> details { get; set; }
public int deviceCount { get; set; }
public string easLastSyncAttempt { get; set; }
public string easUuid { get; set; }
public string emailAddress { get; set; }
public string emailDomain { get; set; }
public bool employeeOwned { get; set; }
public string homeOperator { get; set; }
public int languageCountryId { get; set; }
public int languageId { get; set; }
public string lastConnectedAt { get; set; }
public string manufacturer { get; set; }
public bool mdmManaged { get; set; }
public int mdmProfileUrlId { get; set; }
public string model { get; set; }
public string name { get; set; }
public bool notifyUser { get; set; }
public string #operator { get; set; }
public int operatorId { get; set; }
public string platform { get; set; }
public string platformType { get; set; }
public int quarantinedStatus { get; set; }
public int regCount { get; set; }
public string regType { get; set; }
public string registeredAt { get; set; }
public string status { get; set; }
public int statusCode { get; set; }
public string userDisplayName { get; set; }
public string userFirstName { get; set; }
public string userLastName { get; set; }
public int userSource { get; set; }
public string userUUID { get; set; }
public int wipeReason { get; set; }
}
}
In my MVC razor view i try to access the data:
#model PostenNorge.Models.JsonObjectModels.listDevicesModel
<b>#Model.messages.message</b>
<br />
<br />
<ul>
#foreach(Device d in Model.devices.device)
{
<li>#d.name - #d.uuid - #d.currentPhoneNumber</li>
}
</ul>
On Device in the foreach i get "The type or namespace name "Device" could not be found".
How can i access the nested class types in my view?
You need to specify the nested name:
#foreach(listDevicesModel.Device d in Model.devices.device)
Or use implicit typing:
#foreach(var d in Model.devices.device)
Personally I'd avoid using nested classes here anyway, unless you really have to. Even if you do really have to, I'd rename the top-level class to follow normal naming conventions, i.e. make it start with a capital letter.
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