我正在使用asp.net mvc和entityframwork。我在我的模型中添加了一个新项目Linq - 到Sql类,我命名为:StickerPrinter.dbml。数据库是现有数据库
我添加了一个表:Verploegen_TEST_Location。
但是现在我尝试添加一个下拉列表。因为Verploegen_TEST_Location有五个位置。
我现在在mvc中有这个:
public ActionResult Index()
{
Verploegen_TEST_Location location = new Verploegen_TEST_Location();
}
如果我在Verploegen_TEST_Location位置上执行f12,我会看到该表的所有属性。但我对Index方法有点挣扎。因为通常我使用这样的DBContext:
public class StickerPrinterContext:DbContext
{
public StickerPrinterContext():base("StickerPrinterContext")
{}
public DbSet<Printer>printers { get; set; }
public DbSet<Vestiging>Vestigingen { get; set; }
public DbSet <Vendor>Vendors { get; set; }
public DbSet<Verploegen_TEST_Location> Locations { get; set; }
}
但我认为现在不需要在stickerPrinterContext中添加表格。
那么使用LINQ的下拉函数构建INdex方法?
谢谢
所以,如果我去Verploegen_TEST_Location,我会看到:
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.[Verploegen TEST$Location]")]
public partial class Verploegen_TEST_Location : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private System.Data.Linq.Binary _timestamp;
private string _Code;
private string _Name;
private string _Name_2;
private string _Address;
我的索引控制器就像这样:
public ActionResult Index()
{
IList<Verploegen_TEST_Location> locationList = new List<Verploegen_TEST_Location>();
var query = from location in db.Locations
select location;
return View(locationList);
}
但如果我现在运行代码,我会收到此错误:
在模型生成期间检测到一个或多个验证错误:
StickerPrinterWeb.DAL.Verploegen_TEST_Location: : EntityType 'Verploegen_TEST_Location' has no key defined. Define the key for this EntityType.
Locations: EntityType: EntitySet 'Locations' is based on type 'Verploegen_TEST_Location' that has no keys defined
所以我所做的就是尝试在模型中添加一个新类:
public class Verploegen_TEST_Location
{
}
然后我会收到错误:
代码说明项目行列抑制状态 CS0260声明类型为“Verploegen_TEST_Location”时缺少部分修饰符;此类型的另一个部分声明存在StickerPrinterWeb 8 18 Active
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
我读过这个:
To conclude, Linq to Sql dbml file is an auto-generated file that you should never really need to look the code. The Visual Designer is powerful enough to handle all modifications possible with Linq to Sql.
但是如何处理INdex方法呢?
你的意思是我必须这样做?
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.[Verploegen TEST$Location]")]
public partial class Verploegen_TEST_Location : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private System.Data.Linq.Binary _timestamp;
[Key]
private string _Code;
private string _Name;
private string _Name_2;
private string _Address;
因为上面的文件我看到了:
#pragma warning disable 1591
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace StickerPrinterWeb.Models
我有一个像这样的viewModel:
public class Vm_LocationsList
{
[Key]
public string _Code { get; set; }
public string Name { get; set; }
}
我现在就这样:
public ActionResult Index()
{
// declare your view model if you have any
var vm_LocationList = new Vm_LocationsList();
var locationList = new List<Verploegen_TEST_Location>();
var query = (from location in db.Locations
select location).ToList();
var selectList = new List<SelectListItem>();
foreach (var items in query)
{
selectList.Add(new SelectListItem { Text = items.Name, Value = items.Code });
}
model.SelectList = selectList;
return View(model);
}
但是模型的贬值在哪里?
好的,谢谢。但所以我没有必要编辑
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.[Verploegen TEST$Location]")]
public partial class Verploegen_TEST_Location : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private System.Data.Linq.Binary _timestamp;
[Key]
private string _Code;
我先做了什么?
但我仍然得到错误:
StickerPrinterWeb.DAL.Verploegen_TEST_Location: : EntityType 'Verploegen_TEST_Location' has no key defined. Define the key for this EntityType.
清晰度。这是我的Verploegen_TEST_Location:类的一部分:
#pragma warning disable 1591
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace StickerPrinterWeb.Models
{
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Data;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Linq.Expressions;
using System.ComponentModel;
using System;
using System.ComponentModel.DataAnnotations;
[global::System.Data.Linq.Mapping.DatabaseAttribute(Name="VERPLOEGEN-NAV2009R2-TEST")]
public partial class StickerPrinterDataContext : System.Data.Linq.DataContext
{
private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
#region Extensibility Method Definitions
partial void OnCreated();
partial void InsertVerploegen_TEST_Location(Verploegen_TEST_Location instance);
partial void UpdateVerploegen_TEST_Location(Verploegen_TEST_Location instance);
partial void DeleteVerploegen_TEST_Location(Verploegen_TEST_Location instance);
#endregion
public StickerPrinterDataContext() :
base(global::System.Configuration.ConfigurationManager.ConnectionStrings["VERPLOEGEN_NAV2009R2_TESTConnectionString"].ConnectionString, mappingSource)
{
OnCreated();
}
public StickerPrinterDataContext(string connection) :
base(connection, mappingSource)
{
OnCreated();
}
public StickerPrinterDataContext(System.Data.IDbConnection connection) :
base(connection, mappingSource)
{
OnCreated();
}
public StickerPrinterDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public StickerPrinterDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public System.Data.Linq.Table<Verploegen_TEST_Location> Verploegen_TEST_Locations
{
get
{
return this.GetTable<Verploegen_TEST_Location>();
}
}
}
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.[Verploegen TEST$Location]")]
public partial class Verploegen_TEST_Location : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private System.Data.Linq.Binary _timestamp;
[Key]
private string _Code;
private string _Name;
private string _Name_2;
private string _Address;
private string _Address_2;
private string _City;
private string _Phone_No_;
private string _Phone_No__2;
private string _Telex_No_;
private string _Fax_No_;
private string _Contact;
private string _Post_Code;
private string _County;
private string _E_Mail;
private string _Home_Page;
private string _Country_Region_Code;
private byte _Use_As_In_Transit;
private byte _Require_Put_away;
private byte _Require_Pick;
private string _Cross_Dock_Due_Date_Calc_;
private byte _Use_Cross_Docking;
private byte _Require_Receive;
private byte _Require_Shipment;
private byte _Bin_Mandatory;
private byte _Directed_Put_away_and_Pick;
private int _Default_Bin_Selection;
private string _Outbound_Whse__Handling_Time;
private string _Inbound_Whse__Handling_Time;
private string _Put_away_Template_Code;
private byte _Use_Put_away_Worksheet;
private byte _Pick_According_to_FEFO;
private byte _Allow_Breakbulk;
private int _Bin_Capacity_Policy;
private string _Open_Shop_Floor_Bin_Code;
private string _Inbound_Production_Bin_Code;
private string _Outbound_Production_Bin_Code;
private string _Adjustment_Bin_Code;
private byte _Always_Create_Put_away_Line;
private byte _Always_Create_Pick_Line;
private int _Special_Equipment;
private string _Receipt_Bin_Code;
private string _Shipment_Bin_Code;
private string _Cross_Dock_Bin_Code;
private string _Outbound_BOM_Bin_Code;
private string _Inbound_BOM_Bin_Code;
private string _Base_Calendar_Code;
private byte _Use_ADCS;
private string _Counter_Nos_;
private string _Payment_Method_comb_;
private string _Account_No__Cash;
private string _Account_No__Pay_Card;
private string _Account_No__Cash_Deliver;
private string _Account_No__Pay_Card_Deliver;
private decimal _X_coord;
private decimal _Y_coord;
private byte _Autom__Create_Whse__Shipment;
private byte _Block_for_Item_Availability;
private string _Printer_Name;
答案 0 :(得分:0)
我查看了您的第一条错误消息:
Locations: EntityType: EntitySet 'Locations' is based on type 'Verploegen_TEST_Location' that has no keys defined
根据我的经验,请确保在所有DBML模型类主键字段/属性中添加KeyAttribute
以在DbSet
上下文中使用它们(可能需要在DBML设计器文件上手动编辑,或者在底层数据库上添加主键,然后重新生成DBML结构。)
关于第二条错误消息:
CS0260: Missing partial modifier on declaration of type 'Verploegen_TEST_Location'; another partial declaration of this type exists
由于模型类被声明为partial,因此您无需在其上添加其他相同的模型类而不使用partial修饰符,但您可以创建单独的视图模型以与视图进行通信并将CRUD执行到模型类中。
如果您已经在所有DBML表类上设置了主键字段/属性,则可以尝试使用List<SelectListItem>
或Html.DropDownList
将下拉列表内容作为Html.DropDownListFor
传递到视图中。
编辑:我修改了给定的示例以适合您的代码说明,包括Model&amp;控制器例程在这里:
// Model
public class Vm_LocationsList
{
[Key]
public string _Code { get; set; }
public string Name { get; set; }
// add this line to include drop down list items to your view
public List<SelectListItem> SelectList { get; set; }
}
// Controller
public ActionResult Index()
{
// this is your view model declaration
var vm_LocationList = new Vm_LocationsList();
var locationList = new List<Verploegen_TEST_Location>();
var query = (from location in db.Locations
select location).ToList();
var selectList = new List<SelectListItem>();
foreach (var items in query)
{
selectList.Add(new SelectListItem { Text = items.Name, Value = items.Code });
}
vm_LocationList.SelectList = selectList;
return View(vm_LocationList);
}
// View (strongly-typed version)
@Html.DropDownListFor(m => m.Code, Model.SelectList)
欢迎任何建议。