0

I create a view as you can see :

CREATE VIEW [dbo].[ViewMTO]
AS
    SELECT        
        dbo.Lines.Unit, dbo.Lines.LineNumber, dbo.Lines.DocumentNumber, 
        dbo.BaseMaterials.Name AS MaterialName, 
        dbo.MaterialDescriptions.Name AS MaterialDescription, 
        dbo.MaterialDescriptions.Description, 
        dbo.MaterialScopes.ScopeName, dbo.MaterialScopeObjectNames.ObjectName, 
        dbo.MaterialDescriptions.Size1, dbo.MaterialDescriptions.Size2, 
        dbo.MaterialDescriptions.ItemCode, dbo.Materials.Quantity, 
        dbo.Materials.Discipline
    FROM            
        dbo.Materials 
    INNER JOIN
        dbo.Lines ON dbo.Materials.LineId = dbo.Lines.Id 
    INNER JOIN
        dbo.BaseMaterials ON dbo.Lines.BaseMaterialId = dbo.BaseMaterials.Id 
    INNER JOIN
        dbo.MaterialDescriptions ON dbo.Materials.MaterialDescriptionId = dbo.MaterialDescriptions.Id 
    INNER JOIN
        dbo.MaterialScopes ON dbo.MaterialDescriptions.MaterialScopeId = dbo.MaterialScopes.Id 
    INNER JOIN
        dbo.MaterialScopeObjectNames ON dbo.MaterialDescriptions.MaterialScopeObjectId = dbo.MaterialScopeObjectNames.Id

In my application I want to call it as you can see :

public List<DAViewMTO> ViewMTOByDetail()
{
    List<DAViewMTO> lst = new List<DAViewMTO>();
    lst = _ctx.Database.SqlQuery<DAViewMTO>("SELECT * FROM [SPMS2].[dbo].ViewMTO").ToList();
    return lst;
}

But this query never returns any result. I put a break point in this function and the break point never passes this line:

 lst = _ctx.Database.SqlQuery<DAViewMTO>("SELECT * FROM [SPMS2].[dbo].ViewMTO").ToList();

I have another views with another functions that have this structure and don't have any problem.When i execute my query directly in the SQL it doesn't have any problems.

I call it in the UI:

private void frmViewMaterialTakeOf_Load(object sender, EventArgs e)
    {
        splashScreenManager1.ShowWaitForm();
        gridControl.DataSource = new BindingList<DAViewMTO>(_materialRepository.ViewMTOByDetail()) { AllowNew = true };
        splashScreenManager1.CloseWaitForm();

    }
16
  • Why do you specify the name of the database [SPMS2] in your query ? Shouldn't that be the role of the connection string ? Second, if the function ViewMTOByDetail never gets called, then you need to show where you're calling it. Commented Aug 30, 2016 at 5:24
  • SPMS2 is the name of database Commented Aug 30, 2016 at 5:25
  • 1
    I know it is the name of the database. But you don't need it. You already specify it in the connection string. Why are you writing a raw SQL query ? You can map the view directly through Entity Framework and then call it like this: _ctx.ViewMTO.ToList(); Just like you map any other physical table. Commented Aug 30, 2016 at 5:27
  • @user3185569 i am using entity code first so how can i call this view like this :_ctx.ViewMTO.ToList(); Commented Aug 30, 2016 at 5:28
  • 1
    lst = _ctx.Database.SqlQuery<DAViewMTO>("SELECT * FROM [SPMS2].[dbo].ViewMTO").ToList(); Can you enable all exceptions? maybe you can find any usefull error message which is not redirected to your code. Commented Aug 30, 2016 at 13:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.