locked
Problems in adding the entity related to another entity 1 to 0..1 RRS feed

  • Question

  • Hi Everybody,

    I've been problems in saving entity who is related to another entity. This relationship among them is 1 to 0..1.

    See the class:

     [Table("ADPB411")]
        public class File
        {
            [Key]
            [Column("nCdCabecalho")]
            [Required(ErrorMessage = "Id is required.")]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public decimal Id { get; set; }
    
            [Column("nCdDocumento")]
            [Required(ErrorMessage = "Id of document is required.")]
            public decimal DocumentId { get; set; }
    
            /// <summary>
            /// Size of file.
            /// </summary>
            [Column("iTamanho")]
            [Required(ErrorMessage = "Size is required.")]
            public int Size { get; set; }
    
            /// <summary>
            /// File Name.
            /// </summary>
            [Column("cNmArquivo")]
            [MaxLength(255, ErrorMessage = "Name cannot be longer than 255 characters.")]
            [Required(ErrorMessage = "Name is required.")]
            public string Name { get; set; }
    
            /// <summary>
            /// Contains the type of file.
            /// </summary>
            [Column("cTipo")]
            [MaxLength(10, ErrorMessage = "Type of file cannot be longer than 10 characters.")]
            [Required(ErrorMessage = "Type of file is required.")]
            public string TypeOfFile { get; set; }
    
            /// <summary>
            /// Contains the file's hash.
            /// </summary>
            [Column("cHash")]
            [MaxLength(32, ErrorMessage = "File's hash cannot be longer than 32 characters.")]
            [Required(ErrorMessage = "File's hash is required.")]
            public string FileHash { get; set; }
    
            public virtual Document Document { get; set; }
    
            public virtual FileStream FileStream { get; set; }
    
        }

     [Table("ADPB412")]
        public class FileStream
        {
            [Key]
            [Column("nCdArquivo")]
            [DatabaseGenerated(DatabaseGeneratedOption.None)]
            public decimal Id { get; set; }
    
            /// <summary>
            /// Contains the the binary file.
            /// </summary>
            [Column("biArquivo")]
            [Required(ErrorMessage = "BinaryFile is required.")]
            public byte[] BinaryFile { get; set; }
    
            public virtual File File { get; set; }
        }

    So, there is a File that contails just a file stream. the file stream entity has a primary Id where it is foreign key from File entity.

    See the mapping...

    modelBuilder.Entity<FileStream>()
                    .HasRequired(fs => fs.File)
                    .WithOptional();
    
    modelBuilder.Entity<File>()
                    .HasOptional(f => f.FileStream)
                    .WithRequired();

    The problems is: when add the new file (the file id is identity) with file stream (the id isn't identity) just file id is updated the right value , however the file stream id isn't updated withe the same file value.

    Ex.: 

    File ID         FileStream ID - would be right   FileStream ID - wrong  exception occurs

    0                                      0                                         0

    1                                       1                                        0 

    2                                       2                                        0

    Does anybody know how to update the filestream id when the file is saved?

    Friday, November 23, 2012 6:33 PM

Answers

  • Hi Carlos,

    Welcome to the MSDN forum.

    Please check if the following works:

        public class File
        {
            [Key]
            [Column("nCdCabecalho")]
            [Required(ErrorMessage = "Id is required.")]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public decimal Id { get; set; }
    
            [Column("nCdDocumento")]
            [Required(ErrorMessage = "Id of document is required.")]
            public decimal DocumentId { get; set; }
    
            /// <summary>
            /// Size of file.
            /// </summary>
            [Column("iTamanho")]
            [Required(ErrorMessage = "Size is required.")]
            public int Size { get; set; }
    
            /// <summary>
            /// File Name.
            /// </summary>
            [Column("cNmArquivo")]
            [MaxLength(255, ErrorMessage = "Name cannot be longer than 255 characters.")]
            [Required(ErrorMessage = "Name is required.")]
            public string Name { get; set; }
    
            /// <summary>
            /// Contains the type of file.
            /// </summary>
            [Column("cTipo")]
            [MaxLength(10, ErrorMessage = "Type of file cannot be longer than 10 characters.")]
            [Required(ErrorMessage = "Type of file is required.")]
            public string TypeOfFile { get; set; }
    
            /// <summary>
            /// Contains the file's hash.
            /// </summary>
            [Column("cHash")]
            [MaxLength(32, ErrorMessage = "File's hash cannot be longer than 32 characters.")]
            [Required(ErrorMessage = "File's hash is required.")]
            public string FileHash { get; set; }
    
            public virtual Document Document { get; set; }
    
            public virtual FileStream FileStream { get; set; }
        }
    
        public class FileStream
        {
            [Key]
            [Column("nCdArquivo")]
            public decimal Id { get; set; }
    
            /// <summary>
            /// Contains the the binary file.
            /// </summary>
            [Column("biArquivo")]
            [Required(ErrorMessage = "BinaryFile is required.")]
            public byte[] BinaryFile { get; set; }
        }
    
        public class testEntity : DbContext
        {
            public DbSet<File> Files { get; set; }
            public DbSet<FileStream> FileStream { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<File>()
                                .HasOptional(f => f.FileStream)
                                .WithRequired();
    
            }
        }
    

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Carlos Marchi Wednesday, November 28, 2012 4:09 PM
    Monday, November 26, 2012 6:37 AM

All replies

  • Hi Carlos,

    Welcome to the MSDN forum.

    Please check if the following works:

        public class File
        {
            [Key]
            [Column("nCdCabecalho")]
            [Required(ErrorMessage = "Id is required.")]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public decimal Id { get; set; }
    
            [Column("nCdDocumento")]
            [Required(ErrorMessage = "Id of document is required.")]
            public decimal DocumentId { get; set; }
    
            /// <summary>
            /// Size of file.
            /// </summary>
            [Column("iTamanho")]
            [Required(ErrorMessage = "Size is required.")]
            public int Size { get; set; }
    
            /// <summary>
            /// File Name.
            /// </summary>
            [Column("cNmArquivo")]
            [MaxLength(255, ErrorMessage = "Name cannot be longer than 255 characters.")]
            [Required(ErrorMessage = "Name is required.")]
            public string Name { get; set; }
    
            /// <summary>
            /// Contains the type of file.
            /// </summary>
            [Column("cTipo")]
            [MaxLength(10, ErrorMessage = "Type of file cannot be longer than 10 characters.")]
            [Required(ErrorMessage = "Type of file is required.")]
            public string TypeOfFile { get; set; }
    
            /// <summary>
            /// Contains the file's hash.
            /// </summary>
            [Column("cHash")]
            [MaxLength(32, ErrorMessage = "File's hash cannot be longer than 32 characters.")]
            [Required(ErrorMessage = "File's hash is required.")]
            public string FileHash { get; set; }
    
            public virtual Document Document { get; set; }
    
            public virtual FileStream FileStream { get; set; }
        }
    
        public class FileStream
        {
            [Key]
            [Column("nCdArquivo")]
            public decimal Id { get; set; }
    
            /// <summary>
            /// Contains the the binary file.
            /// </summary>
            [Column("biArquivo")]
            [Required(ErrorMessage = "BinaryFile is required.")]
            public byte[] BinaryFile { get; set; }
        }
    
        public class testEntity : DbContext
        {
            public DbSet<File> Files { get; set; }
            public DbSet<FileStream> FileStream { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<File>()
                                .HasOptional(f => f.FileStream)
                                .WithRequired();
    
            }
        }
    

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Carlos Marchi Wednesday, November 28, 2012 4:09 PM
    Monday, November 26, 2012 6:37 AM
  • Hi Alexander,

    Thank you very much for your help. It worked!!! 

    Wednesday, November 28, 2012 4:09 PM