How to use Linq-to-SQL without having to generate all the classes?

眉间皱痕 提交于 2019-12-10 18:22:44

问题


I have never used Linq-to-Sql before in an application but I used LinqPad to develop a query and just wanted to paste it into into my code.

It's not so simple. I guessed that I need a DataContext to handle the connection but I still get errors because the view names aren't recognised.

Do I have to use the designer and create all the View/Table- classes? Is there a more simple way?

This is the query. Don't need any updates - just this one query....

var q = from user in V020
    join userapp in V010 on user.SPP_USER_ID equals userapp.SPP_USER_ID
    join app in V030 on userapp.SPP_AW_ID equals app.SPP_AW_ID
    join tx in V040 on app.SPP_AW_ID equals tx.SPP_AW_ID
    join appber in V070 on app.SPP_AW_ID equals appber.SPP_AW_ID
    join ber in V050 on appber.SPP_AW_BEREICH_ID equals ber.SPP_AW_BEREICH_ID 

    where app.SPP_AW_AKTIV && user.SPP_USER_ID == "userid" && tx.SPP_AW_SPR == "de" && ber.SPP_AW_SPR == "de"

    orderby ber.SPP_BER_SORTNB

    select new {
        AppName = app.SPP_AW_KURZBEZ, Url = tx.SPP_AW_URL, Label = tx.SPP_AW_NAME, Description = tx.SPP_AW_BESCHR,
        Bereich = ber.SPP_AW_BERNAME,
        Owner = app.SPP_AW_VERANTW, Remote = app.SPP_AW_REMOTE
    }

    ;

回答1:


  1. Create a dbml file, as above
  2. Open the 'server explorer'
  3. Connect to your database
  4. Drag the tables you wish to use from your database connection onto the designer
  5. Party time



回答2:


In EF I have used context.Database.ExecuteSQLCommand to run a stored procedure without the need to use domain modal designer (and hence generating the tables). I am sure you can do something similar in Linq to SQL. However this will not support the linq query you have. You need to move all the logic in stored procedure.

SiteManagedRepairDetails Model properties:

 public long RepairID { get; set; }
 public int RepairLineID { get; set; }

DAL method:

public static int UpsertData(SiteManagedRepairDetails obj)
        {

            XYZ.DBContexts.VoidsDBContext context = new XYZ.DBContexts.VoidsDBContext();
            var results = context.Database.ExecuteSqlCommand("p_XYZDetailsUpsert @RepairID, @RepairLineID"
                , new SqlParameter("@RepairID", obj.RepairID)
                , new SqlParameter("@RepairLineID", obj.RepairLineID)
            );
            return 0;
        }


来源:https://stackoverflow.com/questions/31425715/how-to-use-linq-to-sql-without-having-to-generate-all-the-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!