Using View with Linked Server (Oracle) on Entity Framework (.NET)

One of the issue I came across on Entity Framework was pulling a view that consist of a linked server table (Oracle). When I added the view, I was getting the following error. "The table/view LINKED_SERVER_VIEW does not have a primary key defined and no valid primary key could be inferred. This table/view has been excluded. To use the entity, you will need to review your schema, add the correct keys, and uncomment it." I needed this view as readonly. Only solution that worked was using ISNULL to define a primary key on the view. You will have to create a view like this for Entity Framework to understand the primary key. CREATE VIEW LINKED_SERVER_VIEW AS SELECT ISNULL(PRIMAYKEYCOLUMN, 0) as COL1, OTHERCOLUMN from LINKEDSERVER..SCHEMA.TABLENAME I believe the best approach is to actually fix the table or the model itself and add an actual primary key. However, sometime you will have to work on a database that a client does not want to change and you will have to deal with issues like this. Hope this solution works for you. If you have better solution, feel free to comment.
FacebookTwitterGoogle+Share

Protected Internal access modifiers on C#

Most of us have question on what is a protected internal modifiers. In short, it is a access modifier that can be accessed within the same assembly or by a class that inherits in a different assembly. For testing purposes, we will create an C# solution that has two projects. One of the project is a web application/console application and another is a class library. Instead of application you can create a class library if you wish. We will use the protected, internal and protected internal access modifiers to test the results. On the class library project, I have the following classes Class1 which contains 3 methods with protected, internal and protected internal access modifiers. Class2 does not inherit Class1 but it instantiates Class1 object "b" Class3 inherits Class1 and instantiates object "c" hence inheriting Class1 methods. "b" and "c" will try to access the methods on Class1. On the web application project, I will be using the namespace of class library project above to inherit Class1. Before I do this, I will need to compile class library project above and add the dll as a reference into the web application project. On this web application project, I created the fourth class called Class4. This class will inherit Class1 from the dll. I will try to access the methods on Class1 by creating an object "d". Below is the code Class Library
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ClassLibrary1
{
    public class Class1 //super class that contains the access modifiers and methods
    {
        protected void testprotected() { }
        internal void testinternal() { }
        protected internal void testprotectedinternal() { }
    }
 
    public class Class2 //different class on same assembly
    {
        public void test2()
        {
            Class1 b = new Class1();
            b.testprotected(); //Error 'ClassLibrary1.Class1.testprotected()' is inaccessible due to its protection level
            b.testinternal();
            b.testprotectedinternal();
        }
    }
 
    public class Class3 : Class1 // different class on same assembly that inherits class on same assembly
    {
        public void test3()
        {
            Class3 c = new Class3();
            c.testprotected();
            c.testinternal();
            c.testprotectedinternal();
        }
    }
}
Web Application
using System.Linq;
using System.Web;
using ClassLibrary1;
 
namespace protectedinternaltest1
{
    public class Class4 : Class1 // different class that inherits class from different assembly
    {
        public void test4()
        {
            Class4 d = new Class4();
            d.testprotected();
            d.testinternal(); //Error 'protectedinternaltest1.Class4' does not contain a definition for 'testinternal' and no extension method 'testinternal' accepting a first argument of type 'protectedinternaltest1.Class3' could be found (are you missing a using directive or an assembly reference?)
            d.testprotectedinternal();
        }
    }
}
You will receive the errors(commented above) on the code because of encapsulation. Same Assembly, different class protected: not accessible because protected modifiers allows only sub class to access the protected method internal: accessible because it is within the same assembly protected internal: accessible because it is within the same assembly Same Assembly, sub class (inheritance) protected: accessible because it is a sub class of Class1 internal: accessible protected internal: accessible Different assembly, sub class (inheritance) protected: accessible because protected allows sub class to access the method. internal: inaccessible because it is not within the same class protected internal: accessible because method is called from sub class.