I am trying to follow the example Line Chart / Binding to Local Data. I want to use a view model as my data source. In the view it expects a static method GetElectricityPrices(). How can I make it work with a non-static method?
Here is the view model:
public class ElectricityPrices
{
public int YearPeriod { set; get; }
public double PriceHousehold { set; get; }
public double PriceIndustry { set; get; }
}
Here is the code that is in the view.
#(Html.ShieldChart(<ProjectName>.Controllers.<NameController>.GetElectricityPrices())
.Name("chart")
.Theme("light")
...
Here is a sample static method in the Controller.
public static ElectricityPrices[] GetElectrictyPrices()
{
ElectricityPrices[] prices =
{
new ElectricityPrices { YearPeriod = 2001, PriceHousehold = 0.164, PriceIndustry = 0.103 },
new ElectricityPrices { YearPeriod = 2002, PriceHousehold = 0.173, PriceIndustry = 0.105 },
new ElectricityPrices { YearPeriod = 2003, PriceHousehold = 0.184, PriceIndustry = 0.112 },
new ElectricityPrices { YearPeriod = 2004, PriceHousehold = 0.167, PriceIndustry = 0.111 },
new ElectricityPrices { YearPeriod = 2005, PriceHousehold = 0.177, PriceIndustry = 0.102 },
new ElectricityPrices { YearPeriod = 2006, PriceHousehold = 0.189, PriceIndustry = 0.099 },
new ElectricityPrices { YearPeriod = 2007, PriceHousehold = 0.18, PriceIndustry = 0.011 },
new ElectricityPrices { YearPeriod = 2008, PriceHousehold = 0.183, PriceIndustry = 0.113 },
new ElectricityPrices { YearPeriod = 2009, PriceHousehold = 0.188, PriceIndustry = 0.117 },
new ElectricityPrices { YearPeriod = 2010, PriceHousehold = 0.16, PriceIndustry = 0.119 },
new ElectricityPrices { YearPeriod = 2011, PriceHousehold = 0.176, PriceIndustry = 0.123 },
new ElectricityPrices { YearPeriod = 2012, PriceHousehold = 0.178, PriceIndustry = 0.117 },
};
return prices;
}
The return type of the GetElectrictyPrices() function is IEnumerable<ElectricityPrices>.
The Html.ShieldChart() constructor can accept an IEnumerable collection of any type, which allows you to make a mapping using the .Data(...) method for each series you define to the chart.
Related
I am developing a functionality that adds or removes roles to certain user(s).
I have a problem converting this SQL code into LINQ.
I already tested the SQL and it returns the correct results on SQL Server Management Studio.
Since I am a beginner with LINQ, I find it hard to convert my SQL statements.
/* This sql code works well - returns the users which DO NOT have the role yet in the given company. */
SELECT *
FROM SP.AppUser au
left join
(
SELECT distinct auc.AppUserID
FROM SP.AppUserCompany auc
inner join SP.AppUserCompanyRole aucr ON aucr.AppUserCompanyID = auc.AppUserCompanyID
where auc.CompanyID = 2 and AppRoleID = 4
) AS exist ON exist.AppUserID = au.AppUserID
Where au.AppUserID IN(2088, 38, 1926, 2059, 2058, 1925, 2097, 2061, 2072, 2064)
and exist.AppUserID is null
// Sample code in C# to represent the entities:
AppUser stanley = new AppUser() { AppUserID = 1, FirstName = "Stanley" };
AppUser jerson = new AppUser() { AppUserID = 2, FirstName = "Jerson" };
AppUser philip = new AppUser() { AppUserID = 3, FirstName = "Philip" };
AppUser samantha = new AppUser() { AppUserID = 4, FirstName = "Samantha" };
AppUserCompany auc1 = new AppUserCompany() { AppUserCompanyID = 1, CompanyID = 1, AppUser = stanley };
AppUserCompany auc2 = new AppUserCompany() { AppUserCompanyID = 2, CompanyID = 2, AppUser = stanley };
AppUserCompany auc3 = new AppUserCompany() { AppUserCompanyID = 3, CompanyID = 1, AppUser = jerson };
AppUserCompany auc4 = new AppUserCompany() { AppUserCompanyID = 4, CompanyID = 1, AppUser = philip };
AppUserCompany auc5 = new AppUserCompany() { AppUserCompanyID = 5, CompanyID = 2, AppUser = jerson };
AppUserCompany auc6 = new AppUserCompany() { AppUserCompanyID = 6, CompanyID = 3, AppUser = stanley };
AppUserCompanyRole aucr1 = new AppUserCompanyRole() { AppUserCompanyRoleID = 1, AppUserCompany = auc1, AppRoleID = 1 };
AppUserCompanyRole aucr2 = new AppUserCompanyRole() { AppUserCompanyRoleID = 2, AppUserCompany = auc2, AppRoleID = 1 };
AppUserCompanyRole aucr3 = new AppUserCompanyRole() { AppUserCompanyRoleID = 3, AppUserCompany = auc3, AppRoleID = 1 };
AppUserCompanyRole aucr4 = new AppUserCompanyRole() { AppUserCompanyRoleID = 4, AppUserCompany = auc4, AppRoleID = 1 };
AppUserCompanyRole aucr5 = new AppUserCompanyRole() { AppUserCompanyRoleID = 5, AppUserCompany = auc5, AppRoleID = 1 };
AppUserCompanyRole aucr6 = new AppUserCompanyRole() { AppUserCompanyRoleID = 6, AppUserCompany = auc6, AppRoleID = 1 };
List<AppUser> users = new List<AppUser> { stanley, jerson, philip, samantha };
List<AppUserCompany> appUserCompanies = new List<AppUserCompany> { auc1, auc2, auc3, auc4, auc5, auc6 };
List<AppUserCompanyRole> appUserCompanyRoles = new List<AppUserCompanyRole> { aucr1, aucr2, aucr3, aucr4, aucr5, aucr6 };
// Here is the LINQ code that I tried to get the users "who already have role" in a particular company
var ids = new List<int> { 2088, 38, 1926, 2059, 2058, 1925, 2097, 2061, 2072, 2064 };
var usersToDeleteRole = (from user in appUserDataBinding.Model
where ids.Contains(user.AppUserID)
join auc in appUserCompanyDataBinding.Model on user.AppUserID equals auc.AppUserID into groupJoin1
from subAuc in
(from auc in groupJoin1
join aucr in appUserCompanyRoleDataBinding.Model on auc.AppUserCompanyID equals aucr.AppUserCompanyID
where auc.CompanyID == 2 && aucr.AppRoleID == 4
select auc)
orderby user.LastName
select user).ToList();
My expected result will be a LINQ statement that returns the list of users which who DO NOT have the role yet in a particular given company. The SQL statement I had above worked on SSMS but I cannot make it in LINQ in C# - it means that I need a "negated" version of my LINQ code above.
Thank you for the help.
Cheers.
You can use let statement for you sub queries
public class AppUser
{
public int AppUserID { get; set; }
public string FirstName { get; set; }
}
public class AppUserCompany
{
public int AppUserCompanyID { get; set; }
public int CompanyID { get; set; }
public AppUser AppUser { get; set; }
}
public class AppUserCompanyRole
{
public int AppUserCompanyRoleID { get; set; }
public int AppRoleID { get; set; }
public AppUserCompany AppUserCompany { get; set; }
}
class Program
{
static void Main(string[] args)
{
// Sample code in C# to represent the entities:
AppUser stanley = new AppUser() { AppUserID = 1, FirstName = "Stanley" };
AppUser jerson = new AppUser() { AppUserID = 2, FirstName = "Jerson" };
AppUser philip = new AppUser() { AppUserID = 3, FirstName = "Philip" };
AppUser samantha = new AppUser() { AppUserID = 4, FirstName = "Samantha" };
AppUserCompany auc1 = new AppUserCompany() { AppUserCompanyID = 1, CompanyID = 1, AppUser = stanley };
AppUserCompany auc2 = new AppUserCompany() { AppUserCompanyID = 2, CompanyID = 2, AppUser = stanley };
AppUserCompany auc3 = new AppUserCompany() { AppUserCompanyID = 3, CompanyID = 1, AppUser = jerson };
AppUserCompany auc4 = new AppUserCompany() { AppUserCompanyID = 4, CompanyID = 1, AppUser = philip };
AppUserCompany auc5 = new AppUserCompany() { AppUserCompanyID = 5, CompanyID = 2, AppUser = jerson };
AppUserCompany auc6 = new AppUserCompany() { AppUserCompanyID = 6, CompanyID = 3, AppUser = stanley };
AppUserCompanyRole aucr1 = new AppUserCompanyRole() { AppUserCompanyRoleID = 1, AppUserCompany = auc1, AppRoleID = 1 };
AppUserCompanyRole aucr2 = new AppUserCompanyRole() { AppUserCompanyRoleID = 2, AppUserCompany = auc2, AppRoleID = 1 };
AppUserCompanyRole aucr3 = new AppUserCompanyRole() { AppUserCompanyRoleID = 3, AppUserCompany = auc3, AppRoleID = 1 };
AppUserCompanyRole aucr4 = new AppUserCompanyRole() { AppUserCompanyRoleID = 4, AppUserCompany = auc4, AppRoleID = 1 };
AppUserCompanyRole aucr5 = new AppUserCompanyRole() { AppUserCompanyRoleID = 5, AppUserCompany = auc5, AppRoleID = 1 };
AppUserCompanyRole aucr6 = new AppUserCompanyRole() { AppUserCompanyRoleID = 6, AppUserCompany = auc6, AppRoleID = 1 };
List<AppUser> users = new List<AppUser> { stanley, jerson, philip, samantha };
List<AppUserCompany> appUserCompanies = new List<AppUserCompany> { auc1, auc2, auc3, auc4, auc5, auc6 };
List<AppUserCompanyRole> appUserCompanyRoles = new List<AppUserCompanyRole> { aucr1, aucr2, aucr3, aucr4, aucr5, aucr6 };
var ids = new List<int> { 2088, 38, 1926, 2059, 2058, 1925, 2097, 2061, 2072, 2064 };
var result = from au in users
let appUserIDs = (from auc in appUserCompanies
join aucr in appUserCompanyRoles on auc.AppUserCompanyID equals aucr.AppUserCompany.AppUserCompanyID
where auc.CompanyID == 2 && aucr.AppRoleID == 4 && auc.AppUser.AppUserID == au.AppUserID
select auc.AppUser.AppUserID).Distinct()
where appUserIDs.Count() == 0 && ids.Contains(au.AppUserID)
select au;
I am using Code first migration approach and having problem with populating data.
Database/tables are being created on command update-database but data is not being inserted.
Here is code
internal sealed class Configuration : DbMigrationsConfiguration<webapp_sample.Models.ApplicationDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
//ContextKey = "webapp_sample.Models.ApplicationDbContext";
}
protected override void Seed(webapp_sample.Models.ApplicationDbContext context)
{
context.Companies.AddOrUpdate(p => p.Name,
new Models.Company { CompanyId = 1, Name = "ABC Traders" },
new Models.Company { CompanyId = 2, Name = "XYZ Traders" }
);
context.Divisions.AddOrUpdate(p => p.Name,
new Models.Division { CompanyId = 1, DivisionId = 1, Name = "IT" },
new Models.Division { CompanyId = 1, DivisionId = 2, Name = "Purchasing" },
new Models.Division { CompanyId = 1, DivisionId = 3, Name = "Finance" },
new Models.Division { CompanyId = 1, DivisionId = 4, Name = "Production" },
new Models.Division { CompanyId = 1, DivisionId = 5, Name = "Retail" },
new Models.Division { CompanyId = 2, DivisionId = 6, Name = "HR" },
new Models.Division { CompanyId = 2, DivisionId = 7, Name = "Cafe" },
new Models.Division { CompanyId = 2, DivisionId = 8, Name = "Projects" }
);
context.SubDivisions.AddOrUpdate(p => p.Name,
new Models.SubDivision { CompanyId = 1, DivisionId = 1, SubDivisionId = 1, Name = "Application" },
new Models.SubDivision { CompanyId = 1, DivisionId = 1, SubDivisionId = 2, Name = " Infra" },
new Models.SubDivision { CompanyId = 1, DivisionId = 1, SubDivisionId = 3, Name = "MIS" },
new Models.SubDivision { CompanyId = 1, DivisionId = 2, SubDivisionId = 1, Name = "Purchasing" },
new Models.SubDivision { CompanyId = 1, DivisionId = 3, SubDivisionId = 1, Name = "Finance" },
new Models.SubDivision { CompanyId = 1, DivisionId = 4, SubDivisionId = 1, Name = "Production" },
new Models.SubDivision { CompanyId = 1, DivisionId = 5, SubDivisionId = 1, Name = "Retail" },
new Models.SubDivision { CompanyId = 2, DivisionId = 5, SubDivisionId = 1, Name = "Admin" },
new Models.SubDivision { CompanyId = 2, DivisionId = 6, SubDivisionId = 1, Name = "Cafe" },
new Models.SubDivision { CompanyId = 2, DivisionId = 7, SubDivisionId = 1, Name = "Projects" }
);
}
}
and ApplicationDbContext file as below
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Entity<ApplicationUser>().Property(a => a.Latitude).HasPrecision(18, 9);
modelBuilder.Entity<ApplicationUser>().Property(a => a.Longitude).HasPrecision(18, 9);
base.OnModelCreating(modelBuilder);
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
public DbSet<Company> Companies { get; set; }
public DbSet<Division> Divisions { get; set; }
public DbSet<SubDivision> SubDivisions { get; set; }
public System.Data.Entity.DbSet<webapp_sample.Models.MainMenu> MainMenus { get; set; }
}
Do you use the EF7?
You can create a class:
public class DataInitializer
{
webapp_sample.Models.ApplicationDbContext _context
public void InitializeDataAsync(IServiceProvider serviceProvider)
{
_context = serviceProvider.GetService<webapp_sample.Models.ApplicationDbContext>();
// Add the code for inintializing at here
...
}
}
Also you need to use _context.SaveChanges();.
In the Startup.cs file, you can do somethings as follow:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
...
DataInitializer dataseed = new DataInitializer();
dataseed.InitializeDataAsync(app.ApplicationServices);
}
Hope this help!
I encountered a problem with popup control inside modal edit form of the gridview.
the problem is: popup control won't hide when i click on modal form context area! it just reordered.
please see attached movie first then read the code.
thanks.
here is my code:
MODEL:
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Family { get; set; }
public int Avg { get; set; }
}
CONTROLLER:
public class HomeController : Controller
{
public ActionResult Index()
{
Session["Students"] = new List<Student>()
{
new Student { Id = 1, Name = "N1", Family = "f1", Avg = 1 } ,
new Student { Id = 2, Name = "N2", Family = "f2", Avg = 2 } ,
new Student { Id = 3, Name = "N3", Family = "f3", Avg = 3 } ,
new Student { Id = 4, Name = "N4", Family = "f4", Avg = 4 } ,
};
return View(Session["Students"]);
}
public ActionResult PartialGridView(Student student)
{
return PartialView("Index", Session["Students"]);
}
}
View
#model List<Test.Models.Student>
#Html.DevExpress().GridView(settings =>
{
settings.Name = "GvStudent";
settings.KeyFieldName = "Id";
settings.CallbackRouteValues = new { Controller = "Home", Action = "PartialGridView" };
settings.SettingsEditing.AddNewRowRouteValues = new { Controller = "Home", Action = "AddStudentPartialGridView" };
settings.SettingsEditing.UpdateRowRouteValues = new { Controller = "Home", Action = "UpdateStudentPartialGridView" };
settings.SettingsEditing.DeleteRowRouteValues = new { Controller = "Home", Action = "DeleteStudentPartialGridView" };
settings.SettingsEditing.Mode = GridViewEditingMode.PopupEditForm;
settings.SettingsPopup.EditForm.Modal = true;
settings.SettingsPopup.EditForm.HorizontalAlign = PopupHorizontalAlign.WindowCenter;
settings.SettingsPopup.EditForm.VerticalAlign = PopupVerticalAlign.WindowCenter;
settings.SettingsPopup.EditForm.Width = 400;
settings.SettingsPopup.EditForm.Height = 200;
settings.SettingsText.PopupEditFormCaption = "Student";
settings.CommandColumn.Visible = true;
settings.CommandColumn.NewButton.Visible = true;
settings.CommandColumn.EditButton.Visible = true;
settings.CommandColumn.DeleteButton.Visible = true;
settings.Columns.Add(Name =>
{
Name.FieldName = "Name";
});
settings.Columns.Add(Family =>
{
Family.FieldName = "Family";
});
settings.Columns.Add(Avg =>
{
Avg.FieldName = "Avg";
});
settings.SetEditFormTemplateContent(content =>
{
Html.DevExpress().Button(btnShow =>
{
btnShow.Name="btnShow";
btnShow.ClientSideEvents.Click = "function(s, e){popupControl.ShowAtElement(document.getElementById(s.name));}";
}).Render();
});
}).Bind(Model).GetHtml()
#{
#Html.DevExpress().PopupControl(settings =>
{
settings.Name = "popupControl";
settings.CloseAction = CloseAction.OuterMouseClick;
settings.PopupVerticalAlign = PopupVerticalAlign.Below;
settings.PopupHorizontalAlign = PopupHorizontalAlign.LeftSides;
settings.ShowHeader = false;
settings.ShowFooter = false;
settings.AllowDragging = false;
settings.AutoUpdatePosition = true;
settings.Width = 320;
settings.SetContent(() =>
{
ViewContext.Writer.Write("hello world!");
});
}).GetHtml();
}
Here is my code:
Server-Web API
=======================================
public class OwnerDto {
public int OwnerId { set; get; }
public string OwnerName { set; get; }
}
[HttpGet]
public IEnumerable GetOwner()
{
IEnumerable result = new[] {
new OwnerDto { OwnerId = 1, OwnerName = "Test1" },
new OwnerDto { OwnerId = 2, OwnerName = "Test2" },
new OwnerDto { OwnerId = 3, OwnerName = "Test3" },
new OwnerDto { OwnerId = 4, OwnerName = "Test4" }
};
return result;
}
js code
var dataService = new breeze.DataService({
serviceName: "/api/owner/",
hasServerMetadata: false,
});
var manager = new breeze.EntityManager({ dataService: dataService });
var store = manager.metadataStore;
//metadataStore.namingConvention = namingConv;
store.addEntityType({
shortName: "Owner",
namespace: "Test.Owner",
dataProperties: {
OwnerId: { dataType: breeze.DataType.Int32, isPartOfKey: true },
OwnerName: { dataType: breeze.DataType.String}
}
});
var op = breeze.FilterQueryOp;
var query = new breeze.EntityQuery()
.from("GetOwner")
.where("ownerId",op.Equals,2);
manager.executeQuery(query).then(function (data) {
ko.applyBindings(data, $("#SearchResult")[0]);
}).fail(function (e) {
alert(e);
});
Html Code
p- data-bind="visible: !results" Fetching data ..
ul- data-bind="foreach: results, visible: results" style="display:none"
span- data-bind="text: OwnerName"
span- data-bind="text: OwnerId"
==========================================
The problem is all the data can display but the filter(where/take/order...) does not work.
Any ideas, Thanks very much!
I believe the reason is that the method on your query returns IEnumerable. Those verbs (where/take/order) only apply to service endpoints that return IQueryable<T>. Try this:
[HttpGet]
public IQueryable GetOwner()
{
IEnumerable result = new[] {
new OwnerDto { OwnerId = 1, OwnerName = "Test1" },
new OwnerDto { OwnerId = 2, OwnerName = "Test2" },
new OwnerDto { OwnerId = 3, OwnerName = "Test3" },
new OwnerDto { OwnerId = 4, OwnerName = "Test4" }
};
return result.AsQueryable();
}
I'm new to Entity Framework, which I'm using for an ASP.NET MVC4 project. I'm trying to create a foreign key relationship and am not having much success. The problem may be in my seed data, which I'm not sure how to restructure from an earlier attempt to get this working.
Here are my two classes:
public class HardwareType
{
public int Id { get; set; }
[Required]
[StringLength(128)]
public string HType { get; set; }
public int HardwareId { get; set; }
public virtual Hardware Hardware { get; set; }
}
and
public class Hardware
{
public int Id { get; set; }
//public int HardwareId { get; set; }
...
public virtual ICollection<HardwareType> HardwareType { get; set; }
}
Here is my sample seed data:
protected override void Seed(Context context)
{
var loc = new List<Location> {
new Location { LocationName = "Paradise Lane" },
new Location { LocationName = "81st Street" }
};
loc.ForEach(l => context.Locations.Add(l));
var type = new List<SoftwareType> {
new SoftwareType { SType = "Suite" }
};
type.ForEach(t => context.SoftwareTypes.Add(t));
var pub = new List<SoftwarePublisher> {
new SoftwarePublisher { Publisher = "Adobe" },
new SoftwarePublisher { Publisher = "Apple" },
new SoftwarePublisher { Publisher = "Microsoft" }
};
var soft = new List<Software> {
new Software { Title = "Adobe Creative Suite", Version = "CS6", SerialNumber = "1234634543", Platform = "Mac", Notes = "Macs rock!", PurchaseDate = "2012-12-04", Suite = true, SubscriptionEndDate = null, SeatCount = 0, Locations = loc.Where(s => s.LocationName == "Paradise Lane").ToArray(), Types = type.Where(s => s.SType == "Suite").ToArray(), Publishers = pub.Where(s => s.Publisher == "Adobe").ToArray() },
new Software { Title = "Microsoft Office", Version = "2012", SerialNumber = "34252345", Platform = "PC", Notes = "PCs stink!", PurchaseDate = "2012-11-04", Suite = true, SubscriptionEndDate = null, SeatCount = 0, Locations = loc.Where(s => s.LocationName == "81st Street").ToArray(), Types = type.Where(s => s.SType == "Suite").ToArray(), Publishers = pub.Where(s => s.Publisher == "Microsoft").ToArray() },
new Software { Title = "Apple iLife", Version = "2012", SerialNumber = "54675747564", Platform = "Mac", Notes = "Macs still rock!", PurchaseDate = "2012-12-04", Suite = true, SubscriptionEndDate = null, SeatCount = 0, Locations = loc.Where(s => s.LocationName == "Paradise Lane").ToArray(), Types = type.Where(s => s.SType == "Suite").ToArray(), Publishers = pub.Where(s => s.Publisher == "Apple").ToArray() }
};
soft.ForEach(s => context.Software.Add(s));
var manuf = new List<Manufacturer> {
new Manufacturer { ManufacturerName = "SonicWall" },
new Manufacturer { ManufacturerName = "Cisco" },
new Manufacturer { ManufacturerName = "Barracuda" },
new Manufacturer { ManufacturerName = "Dell" },
new Manufacturer { ManufacturerName = "HP" },
new Manufacturer { ManufacturerName = "Maxtor" },
new Manufacturer { ManufacturerName = "LaCie" },
new Manufacturer { ManufacturerName = "APC" },
new Manufacturer { ManufacturerName = "Intel" },
new Manufacturer { ManufacturerName = "D-Link" },
new Manufacturer { ManufacturerName = "Western Digital" },
new Manufacturer { ManufacturerName = "Quantum" },
new Manufacturer { ManufacturerName = "Seagate" },
new Manufacturer { ManufacturerName = "Apple" },
new Manufacturer { ManufacturerName = "Canon" },
};
var device = new List<DeviceType> {
new DeviceType { DType = "Network Device"},
new DeviceType { DType = "Other"}
};
var htype = new List<HardwareType> {
new HardwareType { HType = "PC" },
new HardwareType { HType = "Monitor" },
new HardwareType { HType = "Printer" },
new HardwareType { HType = "Miscellaneous" }
};
var hard = new List<Hardware> {
new Hardware { AssetTagId = "2134", Type = device.Where(h => h.DType == "Network Device").ToArray(), Manufacturer = manuf.Where(h => h.ManufacturerName == "SonicWall").ToArray(), ServiceTagId = "5243", SerialNumber = "3456", ProductNumber = "2345", PurchaseDate = "2012-10-23", WarrantyExpiration = "2012-11-12", WarrantyType = "NBD", Location = loc.Where(h => h.LocationName == "Paradise Lane").ToArray(), Notes = "Scrapped", HardwareType = htype.Where(h => h.HType == "Monitor").ToArray()},
new Hardware { AssetTagId = "2134", Type = device.Where(h => h.DType == "Network Device").ToArray(), Manufacturer = manuf.Where(h => h.ManufacturerName == "SonicWall").ToArray(), ServiceTagId = "5243", SerialNumber = "3456", ProductNumber = "2345", PurchaseDate = "2012-10-23", WarrantyExpiration = "2012-11-12", WarrantyType = "NBD", Location = loc.Where(h => h.LocationName == "Paradise Lane").ToArray(), Notes = "Scrapped", HardwareType = htype.Where(h => h.HType == "PC").ToArray() },
new Hardware { AssetTagId = "2134", Type = device.Where(h => h.DType == "Network Device").ToArray(), Manufacturer = manuf.Where(h => h.ManufacturerName == "SonicWall").ToArray(), ServiceTagId = "5243", SerialNumber = "3456", ProductNumber = "2345", PurchaseDate = "2012-10-23", WarrantyExpiration = "2012-11-12", WarrantyType = "NBD", Location = loc.Where(h => h.LocationName == "Paradise Lane").ToArray(), Notes = "Scrapped", HardwareType = htype.Where(h => h.HType == "PC").ToArray() }
};
hard.ForEach(h => context.Hardwares.Add(h));
base.Seed(context);
}
How do I structure this so the foreign key relationship works. Right now, I get this error: Unable to set field/property HardwareType on entity type CIT.Models.Hardware. See InnerException for details.
The inner exception is: An item cannot be removed from a fixed size Array of type 'CIT.Models.HardwareType[]'.
try changing you'r tables structure like this :
public class Hardware
{
public int Id { get; set; }
.
.
.
public HardwareTypeId { get; set; }
public virtual HardwareType hardwareType {get;set;}
}
and HardwareType :
public class HardwareType
{
public int Id { get; set; }
public string TypeName {get;set;}
}