以Northwind库为例,新建LINQ TO SQL Classes,我们将表Suppliers和Products拖进新建的dbml文件设计界面。

默认情况下,IDE自动生成的映射关系为1:N.即Cardinality属性为OneToMany即1:N。
如果我们想改为1:1则可选中表关系(图中空心实心箭头),修改Cardinality属性为OneToOne。
但是IDE会报错:Error 1 Cannot create an association "Supplier_Product". Properties do not have matching types: "SupplierID", "SupplierID".
这个错误说明主表主键和从表的外键字段类型不一致,检查发现果然是Products表的外键字段SupplierID为可空类型而主表Suppliers的主键字段为不可空类型。
修改dbml中Products表的SupplierID的Nullable属性为False,然后修改Cardinality属性为OneToOne,成功!
附
1:N的自动生成映射类:
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;
[System.Data.Linq.Mapping.DatabaseAttribute(Name = "Northwind")]
public partial class DataClasses1DataContext : System.Data.Linq.DataContext
{
private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
#region Extensibility Method Definitions
partial void OnCreated();
partial void InsertProduct(Product instance);
partial void UpdateProduct(Product instance);
partial void DeleteProduct(Product instance);
partial void InsertSupplier(Supplier instance);
partial void UpdateSupplier(Supplier instance);
partial void DeleteSupplier(Supplier instance);
#endregion
public DataClasses1DataContext() :
base(global::System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString, mappingSource)
{
OnCreated();
}
public DataClasses1DataContext(string connection) :
base(connection, mappingSource)
{
OnCreated();
}
public DataClasses1DataContext(System.Data.IDbConnection connection) :
base(connection, mappingSource)
{
OnCreated();
}
public DataClasses1DataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public DataClasses1DataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public System.Data.Linq.Table<Product> Products
{
get
{
return this.GetTable<Product>();
}
}
public System.Data.Linq.Table<Supplier> Suppliers
{
get
{
return this.GetTable<Supplier>();
}
}
}
[Table(Name = "dbo.Products")]
public partial class Product : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private int _ProductID;
private string _ProductName;
private System.Nullable<int> _SupplierID;
private System.Nullable<int> _CategoryID;
private string _QuantityPerUnit;
private System.Nullable<decimal> _UnitPrice;
private System.Nullable<short> _UnitsInStock;
private System.Nullable<short> _UnitsOnOrder;
private System.Nullable<short> _ReorderLevel;
private bool _Discontinued;
private EntityRef<Supplier> _Supplier;
#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnProductIDChanging(int value);
partial void OnProductIDChanged();
partial void OnProductNameChanging(string value);
partial void OnProductNameChanged();
partial void OnSupplierIDChanging(System.Nullable<int> value);
partial void OnSupplierIDChanged();
partial void OnCategoryIDChanging(System.Nullable<int> value);
partial void OnCategoryIDChanged();
partial void OnQuantityPerUnitChanging(string value);
partial void OnQuantityPerUnitChanged();
partial void OnUnitPriceChanging(System.Nullable<decimal> value);
partial void OnUnitPriceChanged();
partial void OnUnitsInStockChanging(System.Nullable<short> value);
partial void OnUnitsInStockChanged();
partial void OnUnitsOnOrderChanging(System.Nullable<short> value);
partial void OnUnitsOnOrderChanged();
partial void OnReorderLevelChanging(System.Nullable<short> value);
partial void OnReorderLevelChanged();
partial void OnDiscontinuedChanging(bool value);
partial void OnDiscontinuedChanged();
#endregion
public Product()
{
this._Supplier = default(EntityRef<Supplier>);
OnCreated();
}
[Column(Storage = "_ProductID", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
public int ProductID
{
get
{
return this._ProductID;
}
set
{
if ((this._ProductID != value))
{
this.OnProductIDChanging(value);
this.SendPropertyChanging();
this._ProductID = value;
this.SendPropertyChanged("ProductID");
this.OnProductIDChanged();
}
}
}
[Column(Storage = "_ProductName", DbType = "NVarChar(40) NOT NULL", CanBeNull = false)]
public string ProductName
{
get
{
return this._ProductName;
}
set
{
if ((this._ProductName != value))
{
this.OnProductNameChanging(value);
this.SendPropertyChanging();
this._ProductName = value;
this.SendPropertyChanged("ProductName");
this.OnProductNameChanged();
}
}
}
[Column(Storage = "_SupplierID", DbType = "Int")]
public System.Nullable<int> SupplierID
{
get
{
return this._SupplierID;
}
set
{
if ((this._SupplierID != value))
{
if (this._Supplier.HasLoadedOrAssignedValue)
{
throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
}
this.OnSupplierIDChanging(value);
this.SendPropertyChanging();
this._SupplierID = value;
this.SendPropertyChanged("SupplierID");
this.OnSupplierIDChanged();
}
}
}
[Column(Storage = "_CategoryID", DbType = "Int")]
public System.Nullable<int> CategoryID
{
get
{
return this._CategoryID;
}
set
{
if ((this._CategoryID != value))
{
this.OnCategoryIDChanging(value);
this.SendPropertyChanging();
this._CategoryID = value;
this.SendPropertyChanged("CategoryID");
this.OnCategoryIDChanged();
}
}
}
[Column(Storage = "_QuantityPerUnit", DbType = "NVarChar(20)")]
public string QuantityPerUnit
{
get
{
return this._QuantityPerUnit;
}
set
{
if ((this._QuantityPerUnit != value))
{
this.OnQuantityPerUnitChanging(value);
this.SendPropertyChanging();
this._QuantityPerUnit = value;
this.SendPropertyChanged("QuantityPerUnit");
this.OnQuantityPerUnitChanged();
}
}
}
[Column(Storage = "_UnitPrice", DbType = "Money")]
public System.Nullable<decimal> UnitPrice
{
get
{
return this._UnitPrice;
}
set
{
if ((this._UnitPrice != value))
{
this.OnUnitPriceChanging(value);
this.SendPropertyChanging();
this._UnitPrice = value;
this.SendPropertyChanged("UnitPrice");
this.OnUnitPriceChanged();
}
}
}
[Column(Storage = "_UnitsInStock", DbType = "SmallInt")]
public System.Nullable<short> UnitsInStock
{
get
{
return this._UnitsInStock;
}
set
{
if ((this._UnitsInStock != value))
{
this.OnUnitsInStockChanging(value);
this.SendPropertyChanging();
this._UnitsInStock = value;
this.SendPropertyChanged("UnitsInStock");
this.OnUnitsInStockChanged();
}
}
}
[Column(Storage = "_UnitsOnOrder", DbType = "SmallInt")]
public System.Nullable<short> UnitsOnOrder
{
get
{
return this._UnitsOnOrder;
}
set
{
if ((this._UnitsOnOrder != value))
{
this.OnUnitsOnOrderChanging(value);
this.SendPropertyChanging();
this._UnitsOnOrder = value;
this.SendPropertyChanged("UnitsOnOrder");
this.OnUnitsOnOrderChanged();
}
}
}
[Column(Storage = "_ReorderLevel", DbType = "SmallInt")]
public System.Nullable<short> ReorderLevel
{
get
{
return this._ReorderLevel;
}
set
{
if ((this._ReorderLevel != value))
{
this.OnReorderLevelChanging(value);
this.SendPropertyChanging();
this._ReorderLevel = value;
this.SendPropertyChanged("ReorderLevel");
this.OnReorderLevelChanged();
}
}
}
[Column(Storage = "_Discontinued", DbType = "Bit NOT NULL")]
public bool Discontinued
{
get
{
return this._Discontinued;
}
set
{
if ((this._Discontinued != value))
{
this.OnDiscontinuedChanging(value);
this.SendPropertyChanging();
this._Discontinued = value;
this.SendPropertyChanged("Discontinued");
this.OnDiscontinuedChanged();
}
}
}
[Association(Name = "Supplier_Product", Storage = "_Supplier", ThisKey = "SupplierID", OtherKey = "SupplierID", IsForeignKey = true)]
public Supplier Supplier
{
get
{
return this._Supplier.Entity;
}
set
{
Supplier previousValue = this._Supplier.Entity;
if (((previousValue != value)
|| (this._Supplier.HasLoadedOrAssignedValue == false)))
{
this.SendPropertyChanging();
if ((previousValue != null))
{
this._Supplier.Entity = null;
previousValue.Products.Remove(this);
}
this._Supplier.Entity = value;
if ((value != null))
{
value.Products.Add(this);
this._SupplierID = value.SupplierID;
}
else
{
this._SupplierID = default(Nullable<int>);
}
this.SendPropertyChanged("Supplier");
}
}
}
public event PropertyChangingEventHandler PropertyChanging;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanging()
{
if ((this.PropertyChanging != null))
{
this.PropertyChanging(this, emptyChangingEventArgs);
}
}
protected virtual void SendPropertyChanged(String propertyName)
{
if ((this.PropertyChanged != null))
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
[Table(Name = "dbo.Suppliers")]
public partial class Supplier : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private int _SupplierID;
private string _CompanyName;
private string _ContactName;
private string _ContactTitle;
private string _Address;
private string _City;
private string _Region;
private string _PostalCode;
private string _Country;
private string _Phone;
private string _Fax;
private string _HomePage;
private EntitySet<Product> _Products;
#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnSupplierIDChanging(int value);
partial void OnSupplierIDChanged();
partial void OnCompanyNameChanging(string value);
partial void OnCompanyNameChanged();
partial void OnContactNameChanging(string value);
partial void OnContactNameChanged();
partial void OnContactTitleChanging(string value);
partial void OnContactTitleChanged();
partial void OnAddressChanging(string value);
partial void OnAddressChanged();
partial void OnCityChanging(string value);
partial void OnCityChanged();
partial void OnRegionChanging(string value);
partial void OnRegionChanged();
partial void OnPostalCodeChanging(string value);
partial void OnPostalCodeChanged();
partial void OnCountryChanging(string value);
partial void OnCountryChanged();
partial void OnPhoneChanging(string value);
partial void OnPhoneChanged();
partial void OnFaxChanging(string value);
partial void OnFaxChanged();
partial void OnHomePageChanging(string value);
partial void OnHomePageChanged();
#endregion
public Supplier()
{
this._Products = new EntitySet<Product>(new Action<Product>(this.attach_Products), new Action<Product>(this.detach_Products));
OnCreated();
}
[Column(Storage = "_SupplierID", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
public int SupplierID
{
get
{
return this._SupplierID;
}
set
{
if ((this._SupplierID != value))
{
this.OnSupplierIDChanging(value);
this.SendPropertyChanging();
this._SupplierID = value;
this.SendPropertyChanged("SupplierID");
this.OnSupplierIDChanged();
}
}
}
[Column(Storage = "_CompanyName", DbType = "NVarChar(40) NOT NULL", CanBeNull = false)]
public string CompanyName
{
get
{
return this._CompanyName;
}
set
{
if ((this._CompanyName != value))
{
this.OnCompanyNameChanging(value);
this.SendPropertyChanging();
this._CompanyName = value;
this.SendPropertyChanged("CompanyName");
this.OnCompanyNameChanged();
}
}
}
[Column(Storage = "_ContactName", DbType = "NVarChar(30)")]
public string ContactName
{
get
{
return this._ContactName;
}
set
{
if ((this._ContactName != value))
{
this.OnContactNameChanging(value);
this.SendPropertyChanging();
this._ContactName = value;
this.SendPropertyChanged("ContactName");
this.OnContactNameChanged();
}
}
}
[Column(Storage = "_ContactTitle", DbType = "NVarChar(30)")]
public string ContactTitle
{
get
{
return this._ContactTitle;
}
set
{
if ((this._ContactTitle != value))
{
this.OnContactTitleChanging(value);
this.SendPropertyChanging();
this._ContactTitle = value;
this.SendPropertyChanged("ContactTitle");
this.OnContactTitleChanged();
}
}
}
[Column(Storage = "_Address", DbType = "NVarChar(60)")]
public string Address
{
get
{
return this._Address;
}
set
{
if ((this._Address != value))
{
this.OnAddressChanging(value);
this.SendPropertyChanging();
this._Address = value;
this.SendPropertyChanged("Address");
this.OnAddressChanged();
}
}
}
[Column(Storage = "_City", DbType = "NVarChar(15)")]
public string City
{
get
{
return this._City;
}
set
{
if ((this._City != value))
{
this.OnCityChanging(value);
this.SendPropertyChanging();
this._City = value;
this.SendPropertyChanged("City");
this.OnCityChanged();
}
}
}
[Column(Storage = "_Region", DbType = "NVarChar(15)")]
public string Region
{
get
{
return this._Region;
}
set
{
if ((this._Region != value))
{
this.OnRegionChanging(value);
this.SendPropertyChanging();
this._Region = value;
this.SendPropertyChanged("Region");
this.OnRegionChanged();
}
}
}
[Column(Storage = "_PostalCode", DbType = "NVarChar(10)")]
public string PostalCode
{
get
{
return this._PostalCode;
}
set
{
if ((this._PostalCode != value))
{
this.OnPostalCodeChanging(value);
this.SendPropertyChanging();
this._PostalCode = value;
this.SendPropertyChanged("PostalCode");
this.OnPostalCodeChanged();
}
}
}
[Column(Storage = "_Country", DbType = "NVarChar(15)")]
public string Country
{
get
{
return this._Country;
}
set
{
if ((this._Country != value))
{
this.OnCountryChanging(value);
this.SendPropertyChanging();
this._Country = value;
this.SendPropertyChanged("Country");
this.OnCountryChanged();
}
}
}
[Column(Storage = "_Phone", DbType = "NVarChar(24)")]
public string Phone
{
get
{
return this._Phone;
}
set
{
if ((this._Phone != value))
{
this.OnPhoneChanging(value);
this.SendPropertyChanging();
this._Phone = value;
this.SendPropertyChanged("Phone");
this.OnPhoneChanged();
}
}
}
[Column(Storage = "_Fax", DbType = "NVarChar(24)")]
public string Fax
{
get
{
return this._Fax;
}
set
{
if ((this._Fax != value))
{
this.OnFaxChanging(value);
this.SendPropertyChanging();
this._Fax = value;
this.SendPropertyChanged("Fax");
this.OnFaxChanged();
}
}
}
[Column(Storage = "_HomePage", DbType = "NText", UpdateCheck = UpdateCheck.Never)]
public string HomePage
{
get
{
return this._HomePage;
}
set
{
if ((this._HomePage != value))
{
this.OnHomePageChanging(value);
this.SendPropertyChanging();
this._HomePage = value;
this.SendPropertyChanged("HomePage");
this.OnHomePageChanged();
}
}
}
[Association(Name = "Supplier_Product", Storage = "_Products", ThisKey = "SupplierID", OtherKey = "SupplierID")]
public EntitySet<Product> Products
{
get
{
return this._Products;
}
set
{
this._Products.Assign(value);
}
}
public event PropertyChangingEventHandler PropertyChanging;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanging()
{
if ((this.PropertyChanging != null))
{
this.PropertyChanging(this, emptyChangingEventArgs);
}
}
protected virtual void SendPropertyChanged(String propertyName)
{
if ((this.PropertyChanged != null))
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private void attach_Products(Product entity)
{
this.SendPropertyChanging();
entity.Supplier = this;
}
private void detach_Products(Product entity)
{
this.SendPropertyChanging();
entity.Supplier = null;
}
}
1:1的自动生成映射类:
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;
[System.Data.Linq.Mapping.DatabaseAttribute(Name = "Northwind")]
public partial class DataClasses1DataContext : System.Data.Linq.DataContext
{
private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
#region Extensibility Method Definitions
partial void OnCreated();
partial void InsertSupplier(Supplier instance);
partial void UpdateSupplier(Supplier instance);
partial void DeleteSupplier(Supplier instance);
partial void InsertProduct(Product instance);
partial void UpdateProduct(Product instance);
partial void DeleteProduct(Product instance);
#endregion
public DataClasses1DataContext() :
base(global::System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString, mappingSource)
{
OnCreated();
}
public DataClasses1DataContext(string connection) :
base(connection, mappingSource)
{
OnCreated();
}
public DataClasses1DataContext(System.Data.IDbConnection connection) :
base(connection, mappingSource)
{
OnCreated();
}
public DataClasses1DataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public DataClasses1DataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public System.Data.Linq.Table<Supplier> Suppliers
{
get
{
return this.GetTable<Supplier>();
}
}
public System.Data.Linq.Table<Product> Products
{
get
{
return this.GetTable<Product>();
}
}
}
[Table(Name = "dbo.Suppliers")]
public partial class Supplier : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private int _SupplierID;
private string _CompanyName;
private string _ContactName;
private string _ContactTitle;
private string _Address;
private string _City;
private string _Region;
private string _PostalCode;
private string _Country;
private string _Phone;
private string _Fax;
private string _HomePage;
private EntityRef<Product> _Products;
#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnSupplierIDChanging(int value);
partial void OnSupplierIDChanged();
partial void OnCompanyNameChanging(string value);
partial void OnCompanyNameChanged();
partial void OnContactNameChanging(string value);
partial void OnContactNameChanged();
partial void OnContactTitleChanging(string value);
partial void OnContactTitleChanged();
partial void OnAddressChanging(string value);
partial void OnAddressChanged();
partial void OnCityChanging(string value);
partial void OnCityChanged();
partial void OnRegionChanging(string value);
partial void OnRegionChanged();
partial void OnPostalCodeChanging(string value);
partial void OnPostalCodeChanged();
partial void OnCountryChanging(string value);
partial void OnCountryChanged();
partial void OnPhoneChanging(string value);
partial void OnPhoneChanged();
partial void OnFaxChanging(string value);
partial void OnFaxChanged();
partial void OnHomePageChanging(string value);
partial void OnHomePageChanged();
#endregion
public Supplier()
{
this._Products = default(EntityRef<Product>);
OnCreated();
}
[Column(Storage = "_SupplierID", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
public int SupplierID
{
get
{
return this._SupplierID;
}
set
{
if ((this._SupplierID != value))
{
this.OnSupplierIDChanging(value);
this.SendPropertyChanging();
this._SupplierID = value;
this.SendPropertyChanged("SupplierID");
this.OnSupplierIDChanged();
}
}
}
[Column(Storage = "_CompanyName", DbType = "NVarChar(40) NOT NULL", CanBeNull = false)]
public string CompanyName
{
get
{
return this._CompanyName;
}
set
{
if ((this._CompanyName != value))
{
this.OnCompanyNameChanging(value);
this.SendPropertyChanging();
this._CompanyName = value;
this.SendPropertyChanged("CompanyName");
this.OnCompanyNameChanged();
}
}
}
[Column(Storage = "_ContactName", DbType = "NVarChar(30)")]
public string ContactName
{
get
{
return this._ContactName;
}
set
{
if ((this._ContactName != value))
{
this.OnContactNameChanging(value);
this.SendPropertyChanging();
this._ContactName = value;
this.SendPropertyChanged("ContactName");
this.OnContactNameChanged();
}
}
}
[Column(Storage = "_ContactTitle", DbType = "NVarChar(30)")]
public string ContactTitle
{
get
{
return this._ContactTitle;
}
set
{
if ((this._ContactTitle != value))
{
this.OnContactTitleChanging(value);
this.SendPropertyChanging();
this._ContactTitle = value;
this.SendPropertyChanged("ContactTitle");
this.OnContactTitleChanged();
}
}
}
[Column(Storage = "_Address", DbType = "NVarChar(60)")]
public string Address
{
get
{
return this._Address;
}
set
{
if ((this._Address != value))
{
this.OnAddressChanging(value);
this.SendPropertyChanging();
this._Address = value;
this.SendPropertyChanged("Address");
this.OnAddressChanged();
}
}
}
[Column(Storage = "_City", DbType = "NVarChar(15)")]
public string City
{
get
{
return this._City;
}
set
{
if ((this._City != value))
{
this.OnCityChanging(value);
this.SendPropertyChanging();
this._City = value;
this.SendPropertyChanged("City");
this.OnCityChanged();
}
}
}
[Column(Storage = "_Region", DbType = "NVarChar(15)")]
public string Region
{
get
{
return this._Region;
}
set
{
if ((this._Region != value))
{
this.OnRegionChanging(value);
this.SendPropertyChanging();
this._Region = value;
this.SendPropertyChanged("Region");
this.OnRegionChanged();
}
}
}
[Column(Storage = "_PostalCode", DbType = "NVarChar(10)")]
public string PostalCode
{
get
{
return this._PostalCode;
}
set
{
if ((this._PostalCode != value))
{
this.OnPostalCodeChanging(value);
this.SendPropertyChanging();
this._PostalCode = value;
this.SendPropertyChanged("PostalCode");
this.OnPostalCodeChanged();
}
}
}
[Column(Storage = "_Country", DbType = "NVarChar(15)")]
public string Country
{
get
{
return this._Country;
}
set
{
if ((this._Country != value))
{
this.OnCountryChanging(value);
this.SendPropertyChanging();
this._Country = value;
this.SendPropertyChanged("Country");
this.OnCountryChanged();
}
}
}
[Column(Storage = "_Phone", DbType = "NVarChar(24)")]
public string Phone
{
get
{
return this._Phone;
}
set
{
if ((this._Phone != value))
{
this.OnPhoneChanging(value);
this.SendPropertyChanging();
this._Phone = value;
this.SendPropertyChanged("Phone");
this.OnPhoneChanged();
}
}
}
[Column(Storage = "_Fax", DbType = "NVarChar(24)")]
public string Fax
{
get
{
return this._Fax;
}
set
{
if ((this._Fax != value))
{
this.OnFaxChanging(value);
this.SendPropertyChanging();
this._Fax = value;
this.SendPropertyChanged("Fax");
this.OnFaxChanged();
}
}
}
[Column(Storage = "_HomePage", DbType = "NText", UpdateCheck = UpdateCheck.Never)]
public string HomePage
{
get
{
return this._HomePage;
}
set
{
if ((this._HomePage != value))
{
this.OnHomePageChanging(value);
this.SendPropertyChanging();
this._HomePage = value;
this.SendPropertyChanged("HomePage");
this.OnHomePageChanged();
}
}
}
[Association(Name = "Supplier_Product", Storage = "_Products", ThisKey = "SupplierID", OtherKey = "SupplierID", IsUnique = true, IsForeignKey = false)]
public Product Products
{
get
{
return this._Products.Entity;
}
set
{
Product previousValue = this._Products.Entity;
if (((previousValue != value)
|| (this._Products.HasLoadedOrAssignedValue == false)))
{
this.SendPropertyChanging();
if ((previousValue != null))
{
this._Products.Entity = null;
previousValue.Supplier = null;
}
this._Products.Entity = value;
if ((value != null))
{
value.Supplier = this;
}
this.SendPropertyChanged("Products");
}
}
}
public event PropertyChangingEventHandler PropertyChanging;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanging()
{
if ((this.PropertyChanging != null))
{
this.PropertyChanging(this, emptyChangingEventArgs);
}
}
protected virtual void SendPropertyChanged(String propertyName)
{
if ((this.PropertyChanged != null))
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
[Table(Name = "dbo.Products")]
public partial class Product : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private int _ProductID;
private string _ProductName;
private int _SupplierID;
private System.Nullable<int> _CategoryID;
private string _QuantityPerUnit;
private System.Nullable<decimal> _UnitPrice;
private System.Nullable<short> _UnitsInStock;
private System.Nullable<short> _UnitsOnOrder;
private System.Nullable<short> _ReorderLevel;
private bool _Discontinued;
private EntityRef<Supplier> _Supplier;
#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnProductIDChanging(int value);
partial void OnProductIDChanged();
partial void OnProductNameChanging(string value);
partial void OnProductNameChanged();
partial void OnSupplierIDChanging(int value);
partial void OnSupplierIDChanged();
partial void OnCategoryIDChanging(System.Nullable<int> value);
partial void OnCategoryIDChanged();
partial void OnQuantityPerUnitChanging(string value);
partial void OnQuantityPerUnitChanged();
partial void OnUnitPriceChanging(System.Nullable<decimal> value);
partial void OnUnitPriceChanged();
partial void OnUnitsInStockChanging(System.Nullable<short> value);
partial void OnUnitsInStockChanged();
partial void OnUnitsOnOrderChanging(System.Nullable<short> value);
partial void OnUnitsOnOrderChanged();
partial void OnReorderLevelChanging(System.Nullable<short> value);
partial void OnReorderLevelChanged();
partial void OnDiscontinuedChanging(bool value);
partial void OnDiscontinuedChanged();
#endregion
public Product()
{
this._Supplier = default(EntityRef<Supplier>);
OnCreated();
}
[Column(Storage = "_ProductID", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
public int ProductID
{
get
{
return this._ProductID;
}
set
{
if ((this._ProductID != value))
{
this.OnProductIDChanging(value);
this.SendPropertyChanging();
this._ProductID = value;
this.SendPropertyChanged("ProductID");
this.OnProductIDChanged();
}
}
}
[Column(Storage = "_ProductName", DbType = "NVarChar(40) NOT NULL", CanBeNull = false)]
public string ProductName
{
get
{
return this._ProductName;
}
set
{
if ((this._ProductName != value))
{
this.OnProductNameChanging(value);
this.SendPropertyChanging();
this._ProductName = value;
this.SendPropertyChanged("ProductName");
this.OnProductNameChanged();
}
}
}
[Column(Storage = "_SupplierID", DbType = "Int")]
public int SupplierID
{
get
{
return this._SupplierID;
}
set
{
if ((this._SupplierID != value))
{
if (this._Supplier.HasLoadedOrAssignedValue)
{
throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
}
this.OnSupplierIDChanging(value);
this.SendPropertyChanging();
this._SupplierID = value;
this.SendPropertyChanged("SupplierID");
this.OnSupplierIDChanged();
}
}
}
[Column(Storage = "_CategoryID", DbType = "Int")]
public System.Nullable<int> CategoryID
{
get
{
return this._CategoryID;
}
set
{
if ((this._CategoryID != value))
{
this.OnCategoryIDChanging(value);
this.SendPropertyChanging();
this._CategoryID = value;
this.SendPropertyChanged("CategoryID");
this.OnCategoryIDChanged();
}
}
}
[Column(Storage = "_QuantityPerUnit", DbType = "NVarChar(20)")]
public string QuantityPerUnit
{
get
{
return this._QuantityPerUnit;
}
set
{
if ((this._QuantityPerUnit != value))
{
this.OnQuantityPerUnitChanging(value);
this.SendPropertyChanging();
this._QuantityPerUnit = value;
this.SendPropertyChanged("QuantityPerUnit");
this.OnQuantityPerUnitChanged();
}
}
}
[Column(Storage = "_UnitPrice", DbType = "Money")]
public System.Nullable<decimal> UnitPrice
{
get
{
return this._UnitPrice;
}
set
{
if ((this._UnitPrice != value))
{
this.OnUnitPriceChanging(value);
this.SendPropertyChanging();
this._UnitPrice = value;
this.SendPropertyChanged("UnitPrice");
this.OnUnitPriceChanged();
}
}
}
[Column(Storage = "_UnitsInStock", DbType = "SmallInt")]
public System.Nullable<short> UnitsInStock
{
get
{
return this._UnitsInStock;
}
set
{
if ((this._UnitsInStock != value))
{
this.OnUnitsInStockChanging(value);
this.SendPropertyChanging();
this._UnitsInStock = value;
this.SendPropertyChanged("UnitsInStock");
this.OnUnitsInStockChanged();
}
}
}
[Column(Storage = "_UnitsOnOrder", DbType = "SmallInt")]
public System.Nullable<short> UnitsOnOrder
{
get
{
return this._UnitsOnOrder;
}
set
{
if ((this._UnitsOnOrder != value))
{
this.OnUnitsOnOrderChanging(value);
this.SendPropertyChanging();
this._UnitsOnOrder = value;
this.SendPropertyChanged("UnitsOnOrder");
this.OnUnitsOnOrderChanged();
}
}
}
[Column(Storage = "_ReorderLevel", DbType = "SmallInt")]
public System.Nullable<short> ReorderLevel
{
get
{
return this._ReorderLevel;
}
set
{
if ((this._ReorderLevel != value))
{
this.OnReorderLevelChanging(value);
this.SendPropertyChanging();
this._ReorderLevel = value;
this.SendPropertyChanged("ReorderLevel");
this.OnReorderLevelChanged();
}
}
}
[Column(Storage = "_Discontinued", DbType = "Bit NOT NULL")]
public bool Discontinued
{
get
{
return this._Discontinued;
}
set
{
if ((this._Discontinued != value))
{
this.OnDiscontinuedChanging(value);
this.SendPropertyChanging();
this._Discontinued = value;
this.SendPropertyChanged("Discontinued");
this.OnDiscontinuedChanged();
}
}
}
[Association(Name = "Supplier_Product", Storage = "_Supplier", ThisKey = "SupplierID", OtherKey = "SupplierID", IsForeignKey = true)]
public Supplier Supplier
{
get
{
return this._Supplier.Entity;
}
set
{
Supplier previousValue = this._Supplier.Entity;
if (((previousValue != value)
|| (this._Supplier.HasLoadedOrAssignedValue == false)))
{
this.SendPropertyChanging();
if ((previousValue != null))
{
this._Supplier.Entity = null;
previousValue.Products = null;
}
this._Supplier.Entity = value;
if ((value != null))
{
value.Products = this;
this._SupplierID = value.SupplierID;
}
else
{
this._SupplierID = default(int);
}
this.SendPropertyChanged("Supplier");
}
}
}
public event PropertyChangingEventHandler PropertyChanging;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanging()
{
if ((this.PropertyChanging != null))
{
this.PropertyChanging(this, emptyChangingEventArgs);
}
}
protected virtual void SendPropertyChanged(String propertyName)
{
if ((this.PropertyChanged != null))
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
可以发现,EntitySet这样集概念的属性变成了EntityRef单体的引用属性