none
LinQ -> return List<IMyInterface>: Problem with the code regenerated of the DataContext.cs file. RRS feed

  • Question

  • Auto-generated DataContext File:

    public partial class DBMLDataContext : System.Data.Linq.DataContext
    
    
    
    
    
    
    
    {
    
    
    
    
    
    
    
    	//code skip..........
    
    
    
    
    
    
    
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    public partial class table1:IMyInterface
    
    
    
    
    
    
    
    {
    
    
    
    
    
    
    
    	private string t1_id;
    
    
    
    
    
    
    
    	private string t1_description;
    
    
    
    
    
    
    
    	private int    t1_seq;
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    	// other fields here
    
    
    
    
    
    
    
    	............
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    	private srting table1_yn;
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    	// code skip ....
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    	//interface function here
    
    
    
    
    
    
    
    	public string GetTableId()
    
    
    
    
    
    
    
    	{
    
    
    
    
    
    
    
    		return this.table1_id;
    
    
    
    
    
    
    
    	}	
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    	public string GetDescription()
    
    
    
    
    
    
    
    	{
    
    
    
    
    
    
    
    		return this.t1_description;
    
    
    
    
    
    
    
    	}
    
    
    
    
    
    
    
    	
    
    
    
    
    
    
    
    	Public int GetSeq()
    
    
    
    
    
    
    
    	{
    
    
    
    
    
    
    
    		return this.t1_seq;
    
    
    
    
    
    
    
    	}
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    	public bool isTableYN()
    
    
    
    
    
    
    
        	{
    
    
    
    
    
    
    
            	return ture;
    
    
    
    
    
    
    
        	}
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    } 
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    public partial class table2:IMyInterface
    
    
    
    
    
    
    
    {
    
    
    
    
    
    
    
    	private string t2_id;
    
    
    
    
    
    
    
    	private string t2_description;
    
    
    
    
    
    
    
    	private int    t2_seq;
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    	// other fields here
    
    
    
    
    
    
    
    	............
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    	private srting table1_yn;
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    	// code skip ....
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    	//interface function here
    
    
    
    
    
    
    
    	public string GetTableId()
    
    
    
    
    
    
    
    	{
    
    
    
    
    
    
    
    		return this.table2_id;
    
    
    
    
    
    
    
    	}	
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    	public string GetDescription()
    
    
    
    
    
    
    
    	{
    
    
    
    
    
    
    
    		return this.t2_description;
    
    
    
    
    
    
    
    	}
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    	Public int GetSeq()
    
    
    
    
    
    
    
    	{
    
    
    
    
    
    
    
    		return this.t2_seq;
    
    
    
    
    
    
    
    	}
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    	public bool isTableYN()
    
    
    
    
    
    
    
        	{
    
    
    
    
    
    
    
            	return false;
    
    
    
    
    
    
    
        	}
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    } 
    interface:
    public interface IMyInterface
    
    
    
    
    
    
    
    {
    
    
    
    
    
    
    
        // code skip 
    
    
    
    
    
    }
    call this class:
     public List<IMyInterface> GetObjects(string tableId)
    
    
    
    
    
    
    
     {
    
    
    
    
    
    
    
            var t1 = from a in DBMLDataContext.table1
    
    
    
    
    
    
    
                      where a.t1_id == tableId
    
    
    
    
    
    
    
                       select (IMyInterface) a;
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
            
    
            List<IMyInterface> result = t1.ToList();
    
    
    
    
    
            return result;
    
    
    
    
    
    
    
    }


    The code run without any error, but one concern is that the code we added to the auto-generated file will be lost
    if we change the schema of the table1 or table2 due to the code regeneration.

    warning msg at the first line of the file:
    //------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated by a tool.
    //     Runtime Version:2.0.50727.3074
    //
    //     Changes to this file may cause incorrect behavior and will be lost if
    //     the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------


    My question is that, is it posible to return a list<interface> without implement funtions inside the auto-generated file in such case or any other suggesttion?

    Hope that you know why i talking about.

    thank you

     


    thank you
    • Edited by Micro0418 Wednesday, October 7, 2009 1:21 PM thankyou
    Wednesday, October 7, 2009 10:05 AM

Answers

  • Don't edit the .designer.cs file. The datacontext class and the entity classes are partial classes, so you can just add your IMyInterface related stuff in partial class implementations of the same classes in a separate file.


    Kristofer - Huagati Systems Co., Ltd. - Cool tools for Linq-to-SQL and Entity Framework: www.huagati.com/dbmltools (VS designer add-in), www.huagati.com/L2SProfiler (query profiler for L2S)
    • Marked as answer by Yichun_Feng Tuesday, October 13, 2009 7:08 AM
    Wednesday, October 7, 2009 10:15 AM
    Answerer
  • Just create a new class file (or several) and add your own code in that one instead of editing the .designer.cs file.

    Partial classes are merged together at compile time, so the compiler will still generate just one class but one that contain both the code from the .designer.cs file and the code from your own partial class implementation. The key is just that both have the 'partial' keyword, same name, are members of the same namespace, and don't contain any conflicting declarations...

    In your own file:
    public partial class table1:IMyInterface
    {
      // your own fields, properties, methods etc to support IMyInterface goes here...
    }
    And leave everything in the auto-generated .designer.cs untouched...:

    [Table(Name="dbo.Table1)]
    [DataContract] public partial class table1 : INotifyPropertyChanging, INotifyPropertyChanged { //leave the code in the .designer.cs untouched as it will be overwritten the next time you change anything in the dbml file }

    Kristofer - Huagati Systems Co., Ltd. - Cool tools for Linq-to-SQL and Entity Framework: www.huagati.com/dbmltools (VS designer add-in), www.huagati.com/L2SProfiler (query profiler for L2S)
    • Marked as answer by Yichun_Feng Tuesday, October 13, 2009 7:08 AM
    Wednesday, October 7, 2009 10:36 AM
    Answerer

All replies

  • Don't edit the .designer.cs file. The datacontext class and the entity classes are partial classes, so you can just add your IMyInterface related stuff in partial class implementations of the same classes in a separate file.


    Kristofer - Huagati Systems Co., Ltd. - Cool tools for Linq-to-SQL and Entity Framework: www.huagati.com/dbmltools (VS designer add-in), www.huagati.com/L2SProfiler (query profiler for L2S)
    • Marked as answer by Yichun_Feng Tuesday, October 13, 2009 7:08 AM
    Wednesday, October 7, 2009 10:15 AM
    Answerer
  • Thank you Kristofer ,
      could you pls give a example. thank you evry much.
    thank you
    Wednesday, October 7, 2009 10:21 AM
  • Just create a new class file (or several) and add your own code in that one instead of editing the .designer.cs file.

    Partial classes are merged together at compile time, so the compiler will still generate just one class but one that contain both the code from the .designer.cs file and the code from your own partial class implementation. The key is just that both have the 'partial' keyword, same name, are members of the same namespace, and don't contain any conflicting declarations...

    In your own file:
    public partial class table1:IMyInterface
    {
      // your own fields, properties, methods etc to support IMyInterface goes here...
    }
    And leave everything in the auto-generated .designer.cs untouched...:

    [Table(Name="dbo.Table1)]
    [DataContract] public partial class table1 : INotifyPropertyChanging, INotifyPropertyChanged { //leave the code in the .designer.cs untouched as it will be overwritten the next time you change anything in the dbml file }

    Kristofer - Huagati Systems Co., Ltd. - Cool tools for Linq-to-SQL and Entity Framework: www.huagati.com/dbmltools (VS designer add-in), www.huagati.com/L2SProfiler (query profiler for L2S)
    • Marked as answer by Yichun_Feng Tuesday, October 13, 2009 7:08 AM
    Wednesday, October 7, 2009 10:36 AM
    Answerer