Mapping Entity framework for viewing for reading and for table for CUD

Suppose I have an entity called Debt:

public class Debt
{
    [Key]
    public int Id { get; set; }
    public int Amount { get; set; }
    public int UserId { get; set; }
}

I use Code first, so I just imagine IDbSet<Debt>and use it.

After that, I want to add some security for reading at the database level: I created a view called Debt_Read:

CREATE VIEW Debt_Read AS SELECT * FROM Debt WHERE UserId IN (1,2,3)

Although the body remains simple, in real life this code uses some sql function to retrieve the user ID from the session.

I do not want EF to display mine DbSet<Debt>for reading from VIEW and for writing updates and creating in TABLE.

How can i achieve this?

+4
source share
1 answer

Dapper.
Dapper - -ORM, Stack Overflow.
EF CUD Dapper .

Dapper .
(Debt_Read).

:

public Debt FindAll()    
{
   var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BD"].ConnectionString);

   using (IDbConnection db = conn)
   {
      return db.Query<Debt>("Select * From Debt_Read");
   }
}

, , ef dapper.

https://msdn.microsoft.com/en-us/magazine/mt703432

, !

0

All Articles