Coder Social home page Coder Social logo

oasis.efmapper's People

Contributors

keeper013 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

oasis.efmapper's Issues

Pros. and Cons.

Could you please compare your mapper with others in user-friendly format?

The relationship could not be changed because one or more of the foreign-key properties is non-nullable

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:
image

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?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.