喜欢用linq的自己觉得直接在项目中创建linq to sql类,拖来拖去没有技术含量.手动写mapping吧,又觉得累.真是活生生的矛盾体.呵呵..最后微软还是提供了这样的方式,就是利用sqlmetal工具生成mapping.这样就不用自己写重复的代码,针对修改.省事儿还不失技术含量.废话少说,官方介绍:sqlmetal工具介绍
使用心得:
第一步:打开C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin目录,并在地址栏复制路径
第二步:运行cmd.在cmd中打开刚刚复制的目录路径.
第三步:可就要参考msdn了.我的目的是仅仅是生成所有表的mapping,并生成.cs文件.参考命令是:sqlmetal /server:. /database:db_movie /user:sa /password:***** /code:MoviesModels.cs /language:csharp /namespace:Study.Models
大致意思是打开本服务器的db_movie数据库,生成MovieModels.cs的文件,生成语言是c#,命名空间是Study.Model.
好了,在C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin这个目录下就生成了一个MoviesModels.cs的文件,然后将其copy至开发环境.
View Code
#pragma warning disable 1591 //------------------------------------------------------------------------------ // <auto-generated> // 此代码由工具生成。 // 运行时版本:2.0.50727.5456 // // 对此文件的更改可能会导致不正确的行为,并且如果 // 重新生成代码,这些更改将会丢失。 // </auto-generated> //------------------------------------------------------------------------------ namespace Study.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; #region 模型 [System.Data.Linq.Mapping.DatabaseAttribute(Name = "db_movie")] public partial class Db_movie : System.Data.Linq.DataContext { private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource(); #region Extensibility Method Definitions partial void OnCreated(); partial void InsertT_Forum(T_Forum instance); partial void UpdateT_Forum(T_Forum instance); partial void DeleteT_Forum(T_Forum instance); partial void InsertT_Movie(T_Movie instance); partial void UpdateT_Movie(T_Movie instance); partial void DeleteT_Movie(T_Movie instance); partial void InsertT_UsersMovie(T_UsersMovie instance); partial void UpdateT_UsersMovie(T_UsersMovie instance); partial void DeleteT_UsersMovie(T_UsersMovie instance); partial void InsertT_UsersMovieList(T_UsersMovieList instance); partial void UpdateT_UsersMovieList(T_UsersMovieList instance); partial void DeleteT_UsersMovieList(T_UsersMovieList instance); #endregion /// <summary> /// 构造方法 /// </summary> /// <param name="connection">数据库连接字符串</param> public Db_movie(string connection) : base(connection, mappingSource) { OnCreated(); } public System.Data.Linq.Table<T_Forum> T_Forum { get { return this.GetTable<T_Forum>(); } } public System.Data.Linq.Table<T_Movie> T_Movie { get { return this.GetTable<T_Movie>(); } } public System.Data.Linq.Table<T_UsersMovie> T_UsersMovie { get { return this.GetTable<T_UsersMovie>(); } } public System.Data.Linq.Table<T_UsersMovieList> T_UsersMovieList { get { return this.GetTable<T_UsersMovieList>(); } } } [Table(Name = "dbo.T_Forum")] public partial class T_Forum : INotifyPropertyChanging, INotifyPropertyChanged { private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty); private int _ID; private string _Forum; private EntitySet<T_Movie> _T_Movie; #region Extensibility Method Definitions partial void OnLoaded(); partial void OnValidate(System.Data.Linq.ChangeAction action); partial void OnCreated(); partial void OnIDChanging(int value); partial void OnIDChanged(); partial void OnForumChanging(string value); partial void OnForumChanged(); #endregion public T_Forum() { this._T_Movie = new EntitySet<T_Movie>(new Action<T_Movie>(this.attach_T_Movie), new Action<T_Movie>(this.detach_T_Movie)); OnCreated(); } [Column(Storage = "_ID", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)] public int ID { get { return this._ID; } set { if ((this._ID != value)) { this.OnIDChanging(value); this.SendPropertyChanging(); this._ID = value; this.SendPropertyChanged("ID"); this.OnIDChanged(); } } } [Column(Storage = "_Forum", DbType = "NVarChar(32) NOT NULL", CanBeNull = false)] public string Forum { get { return this._Forum; } set { if ((this._Forum != value)) { this.OnForumChanging(value); this.SendPropertyChanging(); this._Forum = value; this.SendPropertyChanged("Forum"); this.OnForumChanged(); } } } [Association(Name = "FK_T_Movie_T_Forum", Storage = "_T_Movie", OtherKey = "F_ID", DeleteRule = "NO ACTION")] public EntitySet<T_Movie> T_Movie { get { return this._T_Movie; } set { this._T_Movie.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_T_Movie(T_Movie entity) { this.SendPropertyChanging(); entity.T_Forum = this; } private void detach_T_Movie(T_Movie entity) { this.SendPropertyChanging(); entity.T_Forum = null; } } [Table(Name = "dbo.T_Movie")] public partial class T_Movie : INotifyPropertyChanging, INotifyPropertyChanged { private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty); private System.Guid _ID; private string _M_Name; private string _M_Director; private string _M_Playactor; private string _M_Describe; private string _M_Pic; private System.Nullable<int> _F_ID; private EntityRef<T_Forum> _T_Forum; private EntitySet<T_UsersMovieList> _T_UsersMovieList; #region Extensibility Method Definitions partial void OnLoaded(); partial void OnValidate(System.Data.Linq.ChangeAction action); partial void OnCreated(); partial void OnIDChanging(System.Guid value); partial void OnIDChanged(); partial void OnM_NameChanging(string value); partial void OnM_NameChanged(); partial void OnM_DirectorChanging(string value); partial void OnM_DirectorChanged(); partial void OnM_PlayactorChanging(string value); partial void OnM_PlayactorChanged(); partial void OnM_DescribeChanging(string value); partial void OnM_DescribeChanged(); partial void OnM_PicChanging(string value); partial void OnM_PicChanged(); partial void OnF_IDChanging(System.Nullable<int> value); partial void OnF_IDChanged(); #endregion public T_Movie() { this._T_Forum = default(EntityRef<T_Forum>); this._T_UsersMovieList = new EntitySet<T_UsersMovieList>(new Action<T_UsersMovieList>(this.attach_T_UsersMovieList), new Action<T_UsersMovieList>(this.detach_T_UsersMovieList)); OnCreated(); } [Column(Storage = "_ID", DbType = "UniqueIdentifier NOT NULL", IsPrimaryKey = true)] public System.Guid ID { get { return this._ID; } set { if ((this._ID != value)) { this.OnIDChanging(value); this.SendPropertyChanging(); this._ID = value; this.SendPropertyChanged("ID"); this.OnIDChanged(); } } } [Column(Storage = "_M_Name", DbType = "NVarChar(50)")] public string M_Name { get { return this._M_Name; } set { if ((this._M_Name != value)) { this.OnM_NameChanging(value); this.SendPropertyChanging(); this._M_Name = value; this.SendPropertyChanged("M_Name"); this.OnM_NameChanged(); } } } [Column(Storage = "_M_Director", DbType = "NVarChar(512)")] public string M_Director { get { return this._M_Director; } set { if ((this._M_Director != value)) { this.OnM_DirectorChanging(value); this.SendPropertyChanging(); this._M_Director = value; this.SendPropertyChanged("M_Director"); this.OnM_DirectorChanged(); } } } [Column(Storage = "_M_Playactor", DbType = "NVarChar(512)")] public string M_Playactor { get { return this._M_Playactor; } set { if ((this._M_Playactor != value)) { this.OnM_PlayactorChanging(value); this.SendPropertyChanging(); this._M_Playactor = value; this.SendPropertyChanged("M_Playactor"); this.OnM_PlayactorChanged(); } } } [Column(Storage = "_M_Describe", DbType = "NVarChar(2048)")] public string M_Describe { get { return this._M_Describe; } set { if ((this._M_Describe != value)) { this.OnM_DescribeChanging(value); this.SendPropertyChanging(); this._M_Describe = value; this.SendPropertyChanged("M_Describe"); this.OnM_DescribeChanged(); } } } [Column(Storage = "_M_Pic", DbType = "NVarChar(512)")] public string M_Pic { get { return this._M_Pic; } set { if ((this._M_Pic != value)) { this.OnM_PicChanging(value); this.SendPropertyChanging(); this._M_Pic = value; this.SendPropertyChanged("M_Pic"); this.OnM_PicChanged(); } } } [Column(Storage = "_F_ID", DbType = "Int")] public System.Nullable<int> F_ID { get { return this._F_ID; } set { if ((this._F_ID != value)) { if (this._T_Forum.HasLoadedOrAssignedValue) { throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException(); } this.OnF_IDChanging(value); this.SendPropertyChanging(); this._F_ID = value; this.SendPropertyChanged("F_ID"); this.OnF_IDChanged(); } } } [Association(Name = "FK_T_Movie_T_Forum", Storage = "_T_Forum", ThisKey = "F_ID", IsForeignKey = true)] public T_Forum T_Forum { get { return this._T_Forum.Entity; } set { T_Forum previousValue = this._T_Forum.Entity; if (((previousValue != value) || (this._T_Forum.HasLoadedOrAssignedValue == false))) { this.SendPropertyChanging(); if ((previousValue != null)) { this._T_Forum.Entity = null; previousValue.T_Movie.Remove(this); } this._T_Forum.Entity = value; if ((value != null)) { value.T_Movie.Add(this); this._F_ID = value.ID; } else { this._F_ID = default(Nullable<int>); } this.SendPropertyChanged("T_Forum"); } } } [Association(Name = "FK_T_UsersMovieList_T_Movie", Storage = "_T_UsersMovieList", OtherKey = "UML_MID", DeleteRule = "CASCADE")] public EntitySet<T_UsersMovieList> T_UsersMovieList { get { return this._T_UsersMovieList; } set { this._T_UsersMovieList.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_T_UsersMovieList(T_UsersMovieList entity) { this.SendPropertyChanging(); entity.T_Movie = this; } private void detach_T_UsersMovieList(T_UsersMovieList entity) { this.SendPropertyChanging(); entity.T_Movie = null; } } [Table(Name = "dbo.T_UsersMovie")] public partial class T_UsersMovie : INotifyPropertyChanging, INotifyPropertyChanged { private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty); private int _UM_ID; private string _UM_Name; private string _UM_Descript; private string _UM_UserName; private EntitySet<T_UsersMovieList> _T_UsersMovieList; #region Extensibility Method Definitions partial void OnLoaded(); partial void OnValidate(System.Data.Linq.ChangeAction action); partial void OnCreated(); partial void OnUM_IDChanging(int value); partial void OnUM_IDChanged(); partial void OnUM_NameChanging(string value); partial void OnUM_NameChanged(); partial void OnUM_DescriptChanging(string value); partial void OnUM_DescriptChanged(); partial void OnUM_UserNameChanging(string value); partial void OnUM_UserNameChanged(); #endregion public T_UsersMovie() { this._T_UsersMovieList = new EntitySet<T_UsersMovieList>(new Action<T_UsersMovieList>(this.attach_T_UsersMovieList), new Action<T_UsersMovieList>(this.detach_T_UsersMovieList)); OnCreated(); } [Column(Storage = "_UM_ID", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)] public int UM_ID { get { return this._UM_ID; } set { if ((this._UM_ID != value)) { this.OnUM_IDChanging(value); this.SendPropertyChanging(); this._UM_ID = value; this.SendPropertyChanged("UM_ID"); this.OnUM_IDChanged(); } } } [Column(Storage = "_UM_Name", DbType = "NVarChar(32)")] public string UM_Name { get { return this._UM_Name; } set { if ((this._UM_Name != value)) { this.OnUM_NameChanging(value); this.SendPropertyChanging(); this._UM_Name = value; this.SendPropertyChanged("UM_Name"); this.OnUM_NameChanged(); } } } [Column(Storage = "_UM_Descript", DbType = "NVarChar(128)")] public string UM_Descript { get { return this._UM_Descript; } set { if ((this._UM_Descript != value)) { this.OnUM_DescriptChanging(value); this.SendPropertyChanging(); this._UM_Descript = value; this.SendPropertyChanged("UM_Descript"); this.OnUM_DescriptChanged(); } } } [Column(Storage = "_UM_UserName", DbType = "NVarChar(256)")] public string UM_UserName { get { return this._UM_UserName; } set { if ((this._UM_UserName != value)) { this.OnUM_UserNameChanging(value); this.SendPropertyChanging(); this._UM_UserName = value; this.SendPropertyChanged("UM_UserName"); this.OnUM_UserNameChanged(); } } } [Association(Name = "FK_T_UsersMovieList_T_UsersMovie", Storage = "_T_UsersMovieList", OtherKey = "UML_UMID", DeleteRule = "CASCADE")] public EntitySet<T_UsersMovieList> T_UsersMovieList { get { return this._T_UsersMovieList; } set { this._T_UsersMovieList.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_T_UsersMovieList(T_UsersMovieList entity) { this.SendPropertyChanging(); entity.T_UsersMovie = this; } private void detach_T_UsersMovieList(T_UsersMovieList entity) { this.SendPropertyChanging(); entity.T_UsersMovie = null; } } [Table(Name = "dbo.T_UsersMovieList")] public partial class T_UsersMovieList : INotifyPropertyChanging, INotifyPropertyChanged { private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty); private long _UML_ID; private System.Guid _UML_MID; private int _UML_UMID; private EntityRef<T_Movie> _T_Movie; private EntityRef<T_UsersMovie> _T_UsersMovie; #region Extensibility Method Definitions partial void OnLoaded(); partial void OnValidate(System.Data.Linq.ChangeAction action); partial void OnCreated(); partial void OnUML_IDChanging(long value); partial void OnUML_IDChanged(); partial void OnUML_MIDChanging(System.Guid value); partial void OnUML_MIDChanged(); partial void OnUML_UMIDChanging(int value); partial void OnUML_UMIDChanged(); #endregion public T_UsersMovieList() { this._T_Movie = default(EntityRef<T_Movie>); this._T_UsersMovie = default(EntityRef<T_UsersMovie>); OnCreated(); } [Column(Storage = "_UML_ID", AutoSync = AutoSync.OnInsert, DbType = "BigInt NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)] public long UML_ID { get { return this._UML_ID; } set { if ((this._UML_ID != value)) { this.OnUML_IDChanging(value); this.SendPropertyChanging(); this._UML_ID = value; this.SendPropertyChanged("UML_ID"); this.OnUML_IDChanged(); } } } [Column(Storage = "_UML_MID", DbType = "UniqueIdentifier NOT NULL")] public System.Guid UML_MID { get { return this._UML_MID; } set { if ((this._UML_MID != value)) { if (this._T_Movie.HasLoadedOrAssignedValue) { throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException(); } this.OnUML_MIDChanging(value); this.SendPropertyChanging(); this._UML_MID = value; this.SendPropertyChanged("UML_MID"); this.OnUML_MIDChanged(); } } } [Column(Storage = "_UML_UMID", DbType = "Int NOT NULL")] public int UML_UMID { get { return this._UML_UMID; } set { if ((this._UML_UMID != value)) { if (this._T_UsersMovie.HasLoadedOrAssignedValue) { throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException(); } this.OnUML_UMIDChanging(value); this.SendPropertyChanging(); this._UML_UMID = value; this.SendPropertyChanged("UML_UMID"); this.OnUML_UMIDChanged(); } } } [Association(Name = "FK_T_UsersMovieList_T_Movie", Storage = "_T_Movie", ThisKey = "UML_MID", IsForeignKey = true, DeleteOnNull = true)] public T_Movie T_Movie { get { return this._T_Movie.Entity; } set { T_Movie previousValue = this._T_Movie.Entity; if (((previousValue != value) || (this._T_Movie.HasLoadedOrAssignedValue == false))) { this.SendPropertyChanging(); if ((previousValue != null)) { this._T_Movie.Entity = null; previousValue.T_UsersMovieList.Remove(this); } this._T_Movie.Entity = value; if ((value != null)) { value.T_UsersMovieList.Add(this); this._UML_MID = value.ID; } else { this._UML_MID = default(System.Guid); } this.SendPropertyChanged("T_Movie"); } } } [Association(Name = "FK_T_UsersMovieList_T_UsersMovie", Storage = "_T_UsersMovie", ThisKey = "UML_UMID", IsForeignKey = true, DeleteOnNull = true)] public T_UsersMovie T_UsersMovie { get { return this._T_UsersMovie.Entity; } set { T_UsersMovie previousValue = this._T_UsersMovie.Entity; if (((previousValue != value) || (this._T_UsersMovie.HasLoadedOrAssignedValue == false))) { this.SendPropertyChanging(); if ((previousValue != null)) { this._T_UsersMovie.Entity = null; previousValue.T_UsersMovieList.Remove(this); } this._T_UsersMovie.Entity = value; if ((value != null)) { value.T_UsersMovieList.Add(this); this._UML_UMID = value.UM_ID; } else { this._UML_UMID = default(int); } this.SendPropertyChanged("T_UsersMovie"); } } } 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)); } } } #endregion #region Services // public interface IMovieServices { } #endregion } #pragma warning restore 1591
好了数据库的model就建立好了.这里需要注意的是数据库的字段命名规范.比方说我上面的命名就不规范,当然如果时间充裕继续继续修改,这可就比手动写要来的快了...呵呵..