keeper013 / oasis.efmapper Goto Github PK
View Code? Open in Web Editor NEWAn Auto-Mapper like mapper library to map EntityFramework entities and DTOs.
License: MIT License
An Auto-Mapper like mapper library to map EntityFramework entities and DTOs.
License: MIT License
Could you please compare your mapper with others in user-friendly format?
Hi,
I use your library with EF6 and try to save a one to many relationship and getting the following error:
System.InvalidOperationException
HResult=0x80131509
Message=The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
Source=EntityFramework
What I'm trying to do is delete a child entity from its parent and save it
CreateInitialFolderAndFileStructure();
var root = _mapper.GetFolders().FirstOrDefault();
root.Files.RemoveAt(1);
_mapper.AddOrUpdateFolder(root);
Here is the code for mapping and saving DTO to DB:
public void AddOrUpdateFolder(FolderDTO folderDTO)
{
using (var context = new FileSystemEntities())
{
_dbMapper.DatabaseContext = context;
_dbMapper.StartSession();
var mapperTask = _dbMapper.MapAsync<FolderDTO, Folder>(folderDTO, p => p.Include(f => f.Files));
mapperTask.Wait();
_dbMapper.StopSession();
context.SaveChanges();
}
}
And here is how I initialize the Mapper:
public MyEFMapper()
{
_dbMapper = new MapperBuilderFactory()
.MakeMapperBuilder()
.Configure<FolderDTO>()
.SetIdentityPropertyName(nameof(FolderDTO.Id))
.Finish()
.Configure<Folder>()
.SetIdentityPropertyName (nameof(Folder.Id))
.Finish()
.Configure<FileDTO>()
.SetIdentityPropertyName(nameof(FileDTO.Id))
.Finish()
.Configure<File>()
.SetIdentityPropertyName(nameof(File.Id))
.ExcludePropertiesByName(nameof(File.Folder), nameof(File.FolderId))
.Finish()
.Register<FolderDTO, Folder>(MapType.Upsert)
.Register<FileDTO, File>(MapType.Upsert)
.Build()
.MakeToDatabaseMapper();
_memoryMapper = new MapperBuilderFactory()
.MakeMapperBuilder()
.Register<Folder, FolderDTO>()
.Register<Label, LabelDTO>()
.Build()
.MakeToMemoryMapper();
}
My Relationship looks like this:
With this SQL script:
USE [FileSystem]
GO
-- Create a table for FileSystemBase
CREATE TABLE FileSystemBase (
Id INT IDENTITY(1,1) PRIMARY KEY,
ObjectId uniqueidentifier NOT NULL,
Name VARCHAR(255)
);
-- Create a table for Folders
CREATE TABLE Folder (
Id INT PRIMARY KEY REFERENCES FileSystemBase(Id),
ParentFolderId INT NULL FOREIGN KEY REFERENCES Folder(Id),
);
-- Create a table for Files
CREATE TABLE [File] (
Id INT PRIMARY KEY REFERENCES FileSystemBase(Id),
Size INT,
FolderId INT NOT NULL FOREIGN KEY REFERENCES Folder(Id) ON DELETE CASCADE,
);
-- Create a table for FileDetails
CREATE TABLE [FileDetail] (
Id INT IDENTITY(1,1) PRIMARY KEY,
ObjectId uniqueidentifier NOT NULL,
[Key] VARCHAR(255),
[Value] VARCHAR(255),
FileId INT FOREIGN KEY REFERENCES [File](Id) ON DELETE CASCADE,
);
-- Create a table for Labels
CREATE TABLE Label (
Id INT IDENTITY(1,1) PRIMARY KEY,
ObjectId uniqueidentifier NOT NULL,
Name VARCHAR(255)
);
-- Create a junction table for Folder-Label relationship
CREATE TABLE FolderLabel (
FolderId INT NOT NULL,
LabelId INT NOT NULL,
PRIMARY KEY (FolderId, LabelId),
FOREIGN KEY (FolderId) REFERENCES Folder(Id) ON DELETE CASCADE,
FOREIGN KEY (LabelId) REFERENCES Label(Id) ON DELETE CASCADE
);
-- Create a junction table for File-Label relationship
CREATE TABLE FileLabel (
FileId INT NOT NULL,
LabelId INT NOT NULL,
PRIMARY KEY (FileId, LabelId),
FOREIGN KEY (FileId) REFERENCES [File](Id) ON DELETE CASCADE,
FOREIGN KEY (LabelId) REFERENCES Label(Id) ON DELETE CASCADE
);
Could you please help me with what I'm doing wrong?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.