|
i am working with EF code first. so initially i have no tables in database. so i wrote some class and when query those class then i saw EF code first create those tables in db but when i create sql server view in db and later map that view with my code in c# & EF project and when i try to query that view then i was getting error message.
Additional information: The model backing the 'TestDBContext' context has changed since the database was created. Consider using Code First Migrations to update the database
i understand that EF is telling me to do the migration but if i migrate then EF will create that view in db again when the view is in db already exist.
so tell me how could i inform EF that my view is already is in db so migration is not required.
please guide me. thanks
tbhattacharjee
|
|
|
|
|
EF Code first does not support views, and you cannot use views as entities. If you tried to do that, it would explain the message. It's designed so that if you want a view you can define a view model and load it with an IQueryable in order to handle projections, rather than DB side code objects.
If you want to map views, you'll need to should do Database First so that it can lean on an .edmx mapping.
Edit: Corrected content based on Mr Deeming's answer.
"There are three kinds of lies: lies, damned lies and statistics."
- Benjamin Disraeli
modified 13-Sep-16 8:47am.
|
|
|
|
|
Nathan Minier wrote: EF Code first does not support views, and you cannot use views as entities.
Yes it does, and yes you can.
You can't create a view through automatic migrations, but a manual migration with the CREATE / DROP VIEW commands will work fine.
Configure the mapping from the entity to the view, and EF won't know the difference.
The only problem you might encounter is if your view isn't updateable, and you need to add, update or remove entities from it. In that case, you'd need to use MapToStoredProcedures [^] to map the insert, updated and delete commands to stored procedures.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
would you please like to come with code what u said. just post a sample code for handing view in EF code first.
u said "you can define a view model and load it with an IQueryable in order to handle projections, rather than DB side code objects." please show this with sample code. thanks
tbhattacharjee
|
|
|
|
|
Well, Mr Deeming's answer might suit you better, but for a quick and dirty example:
public class User
{
public string UserName {get;set;}
public string PasswordHash {get;set;}
public DateTime Created { get; set; }
public int RoleId { get; set}
[Foreginkey("Role")
public virtual Role Role { get; set;}
}
public class Role
{
public int Id { get; set; }
public string Title { get; set; }
public int GroupId {get; set; }
[ForeignKey("GroupId")]
public virtual Group AssignedGroup {get; set;}
public virtual ICollection<User> Users { get; set; }
}
public class UserViewModel
{
public string UserName {get;set;}
public string Role {get; set;}
public DateTime Created { get; set; }
public UserViewModel(){}
public UserViewModel(User user)
{
UserName = user.UserName;
Role = user.Role.Title;
Created = user.Created;
}
}
public class UserController: ApiController
{
...
public IEnumerable<UserViewModel> GetUsers()
{
return myDbContext.Users.Select(x => new UserViewModel(x)).ToArray();
}
}
"There are three kinds of lies: lies, damned lies and statistics."
- Benjamin Disraeli
|
|
|
|
|
what kind of question i asked and what u answer.
tbhattacharjee
|
|
|
|
|
You'll need to create the view via a manual migration:
public partial class CreateYourViewMigration : DbMigration
{
public override void Up()
{
const string script = @"CREATE VIEW dbo.YourView As ...";
var context = new YourContext();
context.Database.ExecuteSqlCommand(script);
}
public override void Down()
{
const string script = @"DROP VIEW dbo.YourView;";
var context = new YourContext();
context.Database.ExecuteSqlCommand(script);
}
}
Entity Framework Code First Migrations[^]
If your view is not updateable, and you need to add/edit/delete the entities, then you'll need to use MapToStoredProcedures :
Entity Framework Code First Insert/Update/Delete Stored Procedures (EF6 onwards)[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
What is the actual use of resource file in c# or in WCF services ?
|
|
|
|
|
There is a good description of .RESX files here: Resources in .Resx File Format[^] which explains the file format (XML) as well as touching on what it holds.
Basically, it's a human-and-machine readable file which holds the images, icons, text (including - but not restricted to - localisation strings), sounds, and other resources that your app needs to have compiled into it that the compiler uses to build your final EXE or DLL assembly.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
I have a requirement to prevent the user from adding, renaming, or deleting folders and files. I've never done Windows hooks before.
Is this possible? Can someone point me n the right direction?
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
There are no hooks per se for NTFS. You'd be writing a filter driver for NTFS.
Why couldn't you do this with standard NTFS permissions?
|
|
|
|
|
I didn't mention NTFS. I don't really have any preference as to how it's done.
How do I do this with file permissions? Maybe this[^]?
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
What are you doing and why are you hell bent on doing this in code?
|
|
|
|
|
It's a requirement for an app I'm working on
If it's not broken, fix it until it is
|
|
|
|
|
That doesn't answer my questions at all. What's the point of doing this at all?
|
|
|
|
|
To prevent the user from doing anything to app controlled folders and files.
If it's not broken, fix it until it is
|
|
|
|
|
If you're talking about under the Program Files folder you don't have to do anything. Everything under Program Files is ReadOnly to normal users.
But, what already exists depends on where you're these files/folder you're talking about are in the directory tree.
|
|
|
|
|
I have an app that has folders created and files copied to it without the user's interaction. Then permissions are applied.
If the user does not have rights, then they cannot add files, rename them, delete them, or likewise manipulate folders.
If it's not broken, fix it until it is
|
|
|
|
|
Your application will be running as the user that launched it. Your code cannot give more permissions than it already has from the user running it. Also, any changes to NTFS permissions are done as the user running your code. There is NOTHING preventing the user from just putting the permissions back that your code took away.
Permissions are just about never set in application code at run time but set at install time.
|
|
|
|
|
True, but what if I run my code using impersonation? Then the user who created it (my app) would be the only one that could manipulate it, right?
If it's not broken, fix it until it is
|
|
|
|
|
Normal users cannot impersonate other users.
|
|
|
|
|
There has to be a way to prevent a file from being touched by the user. I've see apps that somehow lock the file so no one but that app can manipulate it
If it's not broken, fix it until it is
|
|
|
|
|
And those apps are easily defeated. I've never seen one that couldn't be worked around with just a working knowledge of permissions and, in other cases, how ZIP files work.
So you're saying a user creates a file in your app and saves it and then loses all permissions to manipulate that file later on? That's a very unusual situation.
|
|
|
|
|
No, there's a service running. It detects a file on the server which is then copied to the user's PC.
The user should not be able to do anything to the file, or the folder it's in.
If it's not broken, fix it until it is
|
|
|
|
|
Your application can talk to a service that's running as an account that has sufficient permissions to set/remove permissions on folders.
This is a management nightmare, requiring a good working knowledge of how permissions work and are inherited in the NTFS tree. Such an explanation is beyond the capacity of forums posts.
Now, there's another way. Screw all the management of granting and taking permissions. The service would be the "file manager" in a folder where only the service account has permissions to write files/create folders. Your application could ask the service to save files/create folders as appropriate and the service would do all the file/folder work on behalf of the application using the permissions granted to it by it's own user account. This user account has to be specially setup to login as a service and has the appropriate permissions to where the files/folder will be stored.
|
|
|
|