我使用asp.net mvc與entityframwork。我在模型中添加了一個新項Linq - Sql類,並且我命名爲:StickerPrinter.dbml。該數據庫是一個現有的數據庫從externe數據庫表dropdownlist
我添加了一個表:Verploegen_TEST_Location。
但現在我嘗試添加一個下拉列表。由於Verploegen_TEST_Location有五個位置。
我在MVC現在有這樣的:
public ActionResult Index()
{
Verploegen_TEST_Location location = new Verploegen_TEST_Location();
}
,如果我做F12上Verploegen_TEST_Location位置我看到表的所有屬性。但我有點與索引方法掙扎。因爲通常情況下,我使用的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);
}
,但如果我運行代碼現在,我收到此錯誤:
發現一個或多個驗證錯誤du環模型生成:
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'類型的聲明缺少部分修飾符;這種類型的其他部分存在申報8 StickerPrinterWeb 18主動
//------------------------------------------------------------------------------
// <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.
但如何處理索引方法呢?
你的意思是我必須這樣做?
[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
,我有一個視圖模型是這樣的:
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.
對於cleareness。這是我的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;
你的web.config文件中是否有一個名爲「StickerPrinterContext」的連接字符串?你有什麼錯誤嗎? – Developer
爲數據庫中的Verploegen_TEST_Location表添加主鍵,更新edmx文件,你將會很開心。您不必添加您自己的'公共類Verploegen_TEST_Location' – Developer
但它是現有的數據庫和現有的表。代碼是表中的主鍵:Verploegen_TEST_Location。 – SavantCode