How to save manual change after a update using Entity framework designer? - entity-framework

I'm using entity framework designer to build entities. I found the designer really hard to use because it overwrite all your manually change after each model update using the designer. How did you round off this problem? Thanks!

What sorts of things are you manually changing? The entity still has to be mappable to the database schema.
You can extend or add functionality by declaring a partial class.
Don't make any change to the entities in the generated file -- I think it says that in the header.
All of the entities are generated as partial classes, which means you can declare "more" of the class elsewhere.
Here is an example:
public partial class Name
{
public string First { get; set; }
}
public partial class Name
{
public string Last { get; set; }
}
Although I have two different class declarations, potentially in different files and folders within the project, it gets compiled as one class.
I can now use the Name class like this:
var name = new Name();
name.First = "Jane";
name.Last = "Doe";
As you can see, the properties from both declarations are unified in an object of type Name.
To apply this to EF, leave the partial entity class alone, and declare a separate partial class with the same name to add functionality.

There is an alternative third-party tool. For more information, refer this. Currently, Devart Entity Developer doesn't include the Update From Database functionality. The Update From Database feature is on our roadmap.

Related

retrieving audit history using Z.EntityFramework.Plus.AuditExtensions.Where in DB first?

I'm using Z.EntityFramework.Plus package to implement the audit feature in my project;
I'm following the DB first approach, I run the query that created the two audit tables as described here:
by updating the .edmx file I got those new DBsets
public virtual DbSet<AuditEntries> AuditEntries { get; set; }
public virtual DbSet<AuditEntryProperties> AuditEntryProperties { get; set; }
now I want to retrieve the audit history, I use Where extension method in Z.EntityFramework.Plus namespace:
var auditlog = AuditExtensions.Where<TbAdditions>(oContext.AuditEntries, id).ToList();
but the compiler is yelling out at me, that he can't cast the DBset<AuditEntry> to DBSet<AuditEntries> as in the Where definition it requires DbSet<AuditEntry>, not DbSet<AuditEntries>, should I change the name of the table?
Disclaimer: I'm the owner of the project Entity Framework Plus
You are currently using DB first approach with custom class AuditEntries and AuditEntryProperties. So you are not using Code First with class AuditEntry and AuditEntryProperty provided by Z.EntityFramework.Plus library.
The extension method has been made for the class provided by the library, so that's why it's incompatible with your class.
You will have to implement on your side the same extension methods found on this source: DbSet`AuditEntry.cs
Probably more extension methods will be required.
The Audit feature is supposed to be fully re-coded during the summer to allow more extensibility like this feature.
EDIT: Answer sub-question
How to implement it?
Here is an example how to implement it: https://gist.github.com/mshwf/af017b50ceb2994d953762ef2b20386d

EntityFramwork generating Interfaces for MEF

I am playing around building some buildingblocks based on database tables.
So I've created an UsersManager and a ValidationManager both based on the EDMX "templates".
I'd really like to loose couple those two components with MEF. But therefore i need to create Interfaces of the entityobjects exposed in the ValidationManager.
Is there an easy way of creating those Interfaces, in that manner i can still use the EDMX generated classes?
Thanx,
Paul
Using an example of a database with a Product Table, is this what you're trying to achieve....
but still use generated entity classes (using either the standard EF generator or another POCO generator of some sort).
I'm not sure - as you mention MEF and I don't see it being directly related.
The generated entity classes are partial classes which will allow you to extend the generated class which in this case you want to extend to implement an interface.
Presuming the following interface is going to be used to introduce the layer of abstraction...
public interface IEntity {
public bool IsDeleted { get; set; }
}
Create a new class file with and extended Product class...
public partial class Product : IEntity {
public bool IsDeleted {
get {
throw new NotImplementedException();
}
set {
throw new NotImplementedException();
}
}
}
You have now extended your generated entity Product with the partial class custom code - and you can use it as normal through EF.
Now instead of your UserManager and ValidationManager classes having a hard reference to Product, instead they'll only have reference to IEntity.
If I didn't understand the question, please provide more details on exactly it is you want to do.

Entity Framework Code First internal class - is it possible?

Is it possible to have Code First data classes declared with internal access as shown:
internal class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
I have a requirement that classes and its properties should not be visible outside of the assembly.
As long as your DbContext derived class that exposes your class to EF is in the same assembly, you should be able to. I don't happen to design my apps that way as I prefer more separation. But the context should be able to build the model and it should be able to interact with the classes (e.g. execute queries, save changes etc) if they are in the same assembly since it will have access to the internal class. Even with the various odd things we tried and wrote about in the Code First book, I never happened to try this particular scenario.

ASP.NET MVC | Where I should to put DataAnnotations in model

A models already exists. They are situated in another project. Where I should put DataAnotations in that project or my one? Should I use partial classes? I would like to put DataAnatation because I want javascript validation to work on client.
You can't use partial classes across assemblies, so that option is out.
You can create DTOs (data transfer objects) that are copies of the ones in the other assembly, annotate them and map.
For easy mapping you can use a tool like auto mapper. If the property names match, it will essentially do all the work for you.
Create a partial class for your Model. like this:
[MetadataType(typeof(Log_Validation))]
public partial class Log : IEntity
{
}
then create a new class Log_Validation which does all the data annotations stuff.
public class Log_Validation
{
[DisplayName("Level")]
[Required(ErrorMessage = "Please provide a level")]
public String Level { get; set; }
}

Ria service generated code not support partial class

I'm using Ria service class library. This contains 2 library named RiaClasslibrary RiaClasslibrary.Web.
Riaclasslibrary.Web contains ADO.NET entity data model and named BaseModel. BaseModelcontains tPage class.
My problem is
I'm inserting separated tPage class. This class contains 2 public property. show below
public sealed partial class tPage : EntityObject
{
public List<tPage> Children { get; set; }
public tPage Parent { get; set; }
public Boolean IsSelected { get; set; }
}
After I'm inserting DomainService and building RiaClasslibrary.Web class library. But ria service generated code doesn't contains above properties.
You have a question. Why you separate tPage class. You simply insert those 3 property in Modelbase.Designer code.
My answer is: Database doesn't contain those 3 property and If I'm inserting properties in the code, properties removed after updating Entity Model.
#ebattulga
I don't know if you still have this issue, but I will post the answer because I came to similar issue.
The answer for
After I'm inserting DomainService and
building RiaClasslibrary.Web class
library. But ria service generated
code doesn't contains above
properties.
is quite easy but hard to find.
You can read here in section "Shared Code" http://www.silverlightshow.net/items/WCF-RIA-Services-Part-5-Metadata-and-Shared-Classes.aspx
If you want to see custom properties from partial classes on the Client you have to rename class file name from MyClass.cs to simply MyClass.shared.cs. This will create partial class in the code generated Client side.
HTH
Daniel SkowroĊ„ski