I am building an EF Code First driven application using an existing MSSQL database. I really want to avoid having to use any power tools or wizard based auto-code generation utilities to build my model and dbcontext, as we will need total control over code gen.
My question is, when I build my models (which will end up as dbset() properties in my context), how should I approach the existing SQL columns in my models? Am I required to add a property to my model classes for every db column that is set to “Not Nullable” in the database? Will EF know what to do if I leave out some properties in my model, that corresponding to db columns? Just not sure how this should all work out and am having a bit of trouble finding resources with a direct answer to CF model to existing db field mappings. Does someone have information that would help clarify this process?
Any help would be greatly appreciated, and thank you!
This is the only way I have worked with EF so far and have had no problems. EF will write null values to the DB if the property on your object is null. Also, all of your columns do not need to be represented in your Entity (although obviously all required columns need to be there, or have default values at the DB level if you want to do inserts). I think the reason it is hard to find some info on this these types of questions is this is the stuff ORMs just do, and do well. I would suggest just trying to create and use your first entities, and see what problems you encounter. It will likely be a lot easier than it seems from where you stand now.