ASP.Net MVC3 SQL Nullable - sql

I'm currently working on an ASP.Net MVC3 project. I have it set up so my sql database creates it's tables based off of classes I've created and I need to know if there's a way to set it so that the variables can be null for example:
public class Cart
{
[Key]
public int RecordId { get; set; }
public string CartId { get; set; }
public int VideoId { get; set; }
public int CandyId { get; set; }
public int Count { get; set; }
public System.DateTime DateCreated { get; set; }
public virtual Video Video { get; set; }
public virtual Candy Candy { get; set; }
}
Is there a way to set the VideoId and Candy Id so that they can be null? The way they're coming up right now is as foreign keys to other tables. Any and all help is highly appreciated.

You can make VideoId and CandyId nullable using the ? symbol:
public int? VideoId { get; set; }
public int? CandyId { get; set; }

You have two options:
1) Declare your properties using int?
public int? VideoId { get; set; }
public int? CandyId { get; set; }
2) Declare your properties using regular int, but handling what should happend when you get a null value.
VideoId = thisMightBeNull ?? 0; //?? means, if the variable is null then use 0
Using the first option has the disadventage that if you want to assign VideoId or CandyId to a new variable it should be int?, and sometimes it gets messy.
With the second option you won't have that problem, but it means you should handle what to assign in that moment (default value).

Related

'FK_Ratings_Users_UserId' on table 'Ratings' may cause cycles or multiple cascade paths

I am trying in .NET EFCore the following Code-First migrations through the entities below
User
[Table("Users")]
public class User
{
[Key]
public int Id { get; set; }
[Required]
[MaxLength(100)]
public string FirstName { get; set; }
[Required]
[MaxLength(100)]
public string LastName { get; set; }
[Required]
[MaxLength(250)]
public string Email { get; set; }
[Required]
[MinLength(8), MaxLength(16)]
public string Password { get; set; }
[Required]
[MinLength(6), MaxLength(15)]
public string Phone { get; set; }
public ICollection<Apartment> Apartments { get; set; }
public ICollection<Rating> Ratings { get; set; }
}
Apartment
[Table("Apartments")]
public class Apartment
{
[Key]
public int Id { get; set; }
[Required]
[MinLength(24), MaxLength(100)]
public string Title { get; set; }
[Required]
[MinLength(24), MaxLength(250)]
public string Address { get; set; }
[Required]
public int Price { get; set; }
[ForeignKey("User")]
public int UserId { get; set; }
public User User {get; set;}
public ICollection<Rating> Ratings { get; set; }
public ICollection<AptCateg> AptsCategs { get; set; }
}
Ratings
[Table("Ratings")]
public class Rating
{
[Key]
public int Id { get; set; }
public int Value { get; set; }
[ForeignKey("Apartment")]
public int ApartmentId { get; set; }
public Apartment Apartment { get; set; }
[ForeignKey("User")]
public int UserId { get; set; }
public User User { get; set; }
}
I use the commands dotnet ef migrations add InitialDatabase but when I try to use dotnet ef database update it throws the following error in cmd, as in the title
'FK_Ratings_Users_UserId' on table 'Ratings' may cause cycles or
multiple cascade paths
I tried adding as in the EFCore tutorial from here the modelBuilder's Cascade behavior but it doesn't work because I am getting the same error. I have also tried doing the answer from here but the implementation for HasRequired isn't working even if try to install EntityFrameworkCore.Tools.
I understand that there is an issue with a circular thingy going on. From my intuition the program doesn't know what to do in the case of deleting a user, if to drop or not its ratings and apartments or some of that sort, and this is why its acting this way but I can't fix the problem.
My question is, how can I solve this issue as I cannot create my database, and thus I cannot continue working on the project.
Thanks!
You'll have to make the user relationship optional on one of the tables like:
public int? UserId { get; set; }
Making the property type nullable tells EF that a cascade delete is not required here.
You are causing a cyclic reference by adding the User and Apartment to the Ratings entity. User and Apartment entities already have a one-to-many relationship to the Ratings collection.
'FK_Ratings_Users_UserId' on table 'Ratings' may cause cycles or
multiple cascade paths
This is how the Ratings entity should look like:
[Table("Ratings")]
public class Rating
{
[Key]
public int Id { get; set; }
public int Value { get; set; }
}

Create dynamic Menu constructed from database

I'm working in Asp.Net MVC, and I want to populate menu from database, but I don't really have an idea how can I do it to insert sub-menus into each menu, and each of this sub-menu can have another submenu, etc.
I was is something similar like facebook comment and reply model.
First I have model of menu like:
public int MenuId { get; set; }
public string Name { get; set; }
public string Controller { get; set; }
public string Action { get; set; }
public int Order { get; set; }
public string Icon { get; set; }
public bool IsAdmin { get; set; }
Sub-menu:
public int SubMenuId { get; set; }
public int MenuId { get; set; } // Menu Fk
public string Name { get; set; }
public string Controller { get; set; }
public string Action { get; set; }
public int Order { get; set; }
public string Icon { get; set; }
public bool IsAdmin { get; set; }
It's my logic correctly?, someone implement something similar? What I need to do into controller to make it works?, if someone have an example I really apreciated it. Thanks in Advance
The traditional way is to just have your Menu model and have a nullable ParentMenuId. If the ParentMenuId is null, your Menu is a root node. You can set your navigation properties so from each Menu node you can access .Children to get all of the nodes that point to it.
I believe #Dobbins has the correct idea. In the database you can have a MenuLink table:
MenuLink
- ID int IDENTITY(1,1) NOT NULL <- Primary Key
- ParentId int NULL <- Foreign Key
- OrderNumber int
- LinkUri varchar(1024) <- Can be replaced with Controller, Action
- IconUri varchar(1024)
- Text nvarchar(256)
You can then select all menu links and their sub-links from the database.
Your Menu Object can then have a list of Menu Objects:
public class MenuLink
{
int Id { get; set; }
int ParentId { get; set; }
int OrderNumber { get; set; }
Uri LinkUri { get; set; }
Uri IconUri { get; set; }
string Text { get; set; }
IList<MenuLink> SubMenuItems { get; set; }
}
Now you can use razor, or your JavaScript framework of choice to create the dynamic menu in code.

Saving Data using Entity Framework Code First

I am using ASP.NET MVC Razor Entity Framework Code First C#
Class - A
public class Om_Category
{
[Key]
public int CategoryID { get; set; }
public String CategoryName { get; set; }
public String CategorySanitized { get; set; }
public Boolean IsActive { get; set; }
public DateTime CreationDate { get; set; }
}
Class - B
public class Om_CategorySkills
{
[Key]
public Int32 SkillID { get; set; }
public String Skill { get; set; }
public String SkillSanitized { get; set; }
public DateTime CreationDate { get; set; }
public Boolean IsActive { get; set; }
public Om_Category Category { get; set; }
}
When I try to create the record for table Om_CategorySkills. It says
cannot save the duplicate value in Om_Category table.
This is happening because I am sending the Om_Category class object in Om_CategorySkills class object because there are some fields in Om_Category class that are mandatory.
So I am passing the Om_Category class object also in Om_CategorySkills class object. Is there any way to fix this issue ?
Your navigation properties doesn't seem to be right.. Can you try (I didn't test),
public class Om_Category
{
[Key]
public int CategoryID { get; set; }
public String CategoryName { get; set; }
public String CategorySanitized { get; set; }
public Boolean IsActive { get; set; }
public DateTime CreationDate { get; set; }
public virtual Om_CategorySkills CategorySkills{ get; set; }
}
public class Om_CategorySkills
{
[Key]
public Int32 SkillID { get; set; }
public String Skill { get; set; }
public String SkillSanitized { get; set; }
public DateTime CreationDate { get; set; }
public Boolean IsActive { get; set; }
public int CategoryID {get;set;}
public virtual Om_Category Category { get; set; }
}
I see that your Om_CategorySkills object is lacking an Int32 Om_CategoryId property to be used as foreign key. I would also add a virtual modifier to the navigation property Category, in order to allow for lazy loading.
I think that it may be the case that the category object in your new/edited skill is already in the database, but was not the one retrieved by the context, so the context believes you are trying to save a new category with the Id of an existing one.
You should not try to save a skill object with a category object with no changes. Otherwise, the category object should be the one attached to the context.

ASP.NET MVC - Scaffolding a Drop Down List

I'm trying to learn how to use the ASP.NET MVC 4 and Entity FrameWork 5, and I'm a bit confused by scaffolding for Drop Down Lists.
I have three classes:
public class ScopeType
{
public int ScopeTypeId { get; set; }
[Required]
public string Type { get; set; }
}
public class ScopeManufacturer
{
public int ScopeManufacturerId { get; set; }
[Required]
[Display(Name="Manufacturer Name")]
public string Name { get; set; }
}
public class Scope
{
public int ScopeId { get; set; }
[Required]
public ScopeManufacturer ScopeManufacturer { get; set; }
[Required]
public string Name { get; set; }
[Required]
public ScopeType ScopeType { get; set; }
[Required]
public int Aperture { get; set; }
[Required]
public int FocalLength { get; set; }
}
Essentially, the first two classes are just lists of values that I want to appear in the drop downs on the 'Scope' create/edit forms. It's a 1 to 1 relationship.
I build the solution, and then add scaffolded controllers and views. Unfortunately, for the 'Scope' controller and views, the ScopeType and ScopeManufacturer navigation properties are ignored; no drop down lists are generated.
I then found on Google examples where people describe specifying the relationship between items by creating properties of integers, with the same name as the Id on the related thing. Therefore, I deleted the controllers and views, and tried again with:
public class Scope
{
public int ScopeId { get; set; }
[Required]
public int ScopeManufacturerId { get; set; }
[Required]
public string Name { get; set; }
[Required]
public int ScopeTypeId { get; set; }
[Required]
public int Aperture { get; set; }
[Required]
public int FocalLength { get; set; }
}
This still didn't scaffold drop down lists for me - rather, it gave me 2 extra fields for me to type integers into.
What am I doing wrong, or am I mistaken in believing that the scaffolding in MVC 4 will generate drop down lists for 1 to 1 relationships like that?

How does one get row data from a Silverlight DataGrid?

In Silverlight, how does one get row data from a DataGrid that is full of data?
I have gotten this far (in a method that receives a button click on a row(:
DataGridRow item = (DataGridRow)dg.SelectedItem;
Now, how do I get the individual components of the item that I guess is the selected row?
Help me out here. How do you bind an observablecollection to the grid?
How do you use the cast system when you cast to the object?
When I read the data into the grid, I used this class:
public class Data
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public bool Available { get; set; }
public int index_1 { get; set; }
public int index_2 { get; set; }
public int index_3 { get; set; }
public int index_4 { get; set; }
public int index_5 { get; set; }
public int index_6 { get; set; }
public int index_7 { get; set; }
public int index_8 { get; set; }
public int index_9 { get; set; }
public int index_10 { get; set; }
public int index_11 { get; set; }
public int index_12 { get; set; }
public int index_13 { get; set; }
public int index_14 { get; set; }
public int index_15 { get; set; }
}
So how so I cast when I read back out
This does not work:
Data _mydata = new Data();
YValue = (_mydata.index_1)dg.SelectedItem;
This does not work:
YValue = (index_1)dg.SelectedItem;
This does not work:
YValue = (Data().index_1)dg.SelectedItem;
DataGridRow item = (DataGridRow)dg.SelectedItem;
int index1 = ((Data)item).index_1;
That will give you the value of the first index.
If you have bound a ObservableCollection<Foo> to your grid, your selected item can just be cast into your object - (Foo)dg.SelectedItem
EDIT-- UPDATE TO ANSWER UPDATED QUESTION
The simple answer is, if you are not using MVVM (which I assume by your post your not), in the code behind create a collection (preferably ObservableCollection) of Data and set the grids itemsource property to your collection
public ObservableCollection<Data> MyCollection{get;set;}
void SetGridItemsSource()
{
// populate your collection here, then use the below line to associate it with your
// grids itemssource
MyGrid.ItemsSource = MyCollection;
}
public void GetSelectedItem()
{
//Simply cast the selected item to your type
Data selectedItem = (Data)MyGrid.SelectedItem;
}