Error inserting to database

4 posts, 0 answers
  1. Christine
    Christine avatar
    5 posts
    Member since:
    Jul 2016

    Posted 16 Sep Link to this post

    I'm a bit frustrated...just found out that Telerik Support does not support this product. I am struggling to understand what it is that I am doing wrong here. Hopefully somebody here can help.

    I have a table OrdersWritten that has FK to OrderType. When I try inserting a new record to OrdersWritten, it keeps trying to insert a new record into OrderType.

    Here is code used to add record to database.

    01.public bool Add(OrdersWritten info, string currentUserName)
    02.{
    03.    try
    04.    {
    05.    if (info == null || String.IsNullOrWhiteSpace(info.OrderNumber)
    06.        || String.IsNullOrWhiteSpace(currentUserName))
    07.        return false;
    08.    using (ProductionContext db = new ProductionContext())
    09.    {
    10.        var order = db.WrittenOrders
    11.        .FirstOrDefault(x => x.OrderNumber == info.OrderNumber);
    12.        if (order != null)
    13.        return false;
    14.        order = new OrdersWritten();
    15.        order.CreatedAt = DateTime.Now;
    16.        order.CreatedBy = currentUserName;
    17.        order.OrderNumber = info.OrderNumber;
    18.        order.OrderTypeID = info.OrderTypeID;
    19.        order.OrderWriter = info.OrderWriter;
    20.        order.DateReceived = info.DateReceived;
    21.        order.DateWritten = info.DateWritten;
    22.        order.JobNum = info.JobNum;
    23.        order.JobName = info.JobName;
    24.        order.BatchOrders = info.BatchOrders;
    25.        order.NumCabs = info.NumCabs;
    26.        order.TopsFtg = info.TopsFtg;
    27.        order.OutsourcedTops = info.OutsourcedTops;
    28.        order.Status = 1; 
    29.        db.Add(order);
    30.        db.SaveChanges();
    31.        return true;
    32.    }
    33.    }
    34.    catch (Exception) { throw; }
    35.}

    Here is the mapping code.

    01.private MappingConfiguration<OrderType> GetOrderTypeConfiguration()
    02.{
    03.    var mapping = new MappingConfiguration<OrderType>();
    04.    mapping.MapType(m => new { ID = m.ID, Name = m.Name, Status = m.Status,
    05.        CreatedAt = m.CreatedAt, CreatedBy = m.CreatedBy,
    06.        ModifiedAt = m.ModifiedAt, ModifiedBy = m.ModifiedBy })
    07.    .WithConcurencyControl(OptimisticConcurrencyControlStrategy.Changed)
    08.    .ToTable("OrderType");
    09.    mapping.HasProperty(u => u.ID).HasFieldName("id").IsIdentity(KeyGenerator.Autoinc);
    10.    mapping.HasAssociation(x => x.WrittenOrders)
    11.    .HasFieldName("writtenOrders")
    12.    .WithOpposite(x => x.OrdType)
    13.    .HasConstraint((x, y) => x.ID == y.OrderTypeID)
    14.    .WithDataAccessKind(DataAccessKind.ReadOnly);
    15.    return mapping;
    16.}
    17. 
    18.private MappingConfiguration<OrdersWritten> GetOrdersWrittenConfiguration()
    19.{
    20.    var mapping = new MappingConfiguration<OrdersWritten>();
    21.    mapping.MapType(m => new { ID = m.ID, OrderTypeID = m.OrderTypeID,
    22.        OrderNumber = m.OrderNumber, OrderWriter = m.OrderWriter,
    23.        DateReceived = m.DateReceived, DateWritten = m.DateWritten,
    24.        JobNum = m.JobNum, JobName = m.JobName, BatchOrders = m.BatchOrders,
    25.        NumCabs = m.NumCabs, TopsFtg = m.TopsFtg,
    26.        OutsourcedTops = m.OutsourcedTops,
    27.        Status = m.Status, CreatedAt = m.CreatedAt, CreatedBy = m.CreatedBy,
    28.        ModifiedAt = m.ModifiedAt, ModifiedBy = m.ModifiedBy })
    29.    .WithConcurencyControl(OptimisticConcurrencyControlStrategy.Changed)
    30.    .ToTable("OrdersWritten");
    31.    mapping.HasProperty(u => u.ID).HasFieldName("id")
    32.        .IsIdentity(KeyGenerator.Autoinc);
    33.    mapping.HasAssociation(x => x.OrdType)
    34.    .HasFieldName("ordType")
    35.    .WithOpposite(x => x.WrittenOrders)
    36.    .ToColumn("OrderTypeID")
    37.    .HasConstraint((x, y) => x.OrderTypeID == y.ID)
    38.    .WithDataAccessKind(DataAccessKind.ReadOnly);
    39.    return mapping;
    40.}

     

    I don't understand why it is trying to create a record in OrderType. What am I doing wrong?

    Please help.

  2. Thomas
    Admin
    Thomas avatar
    590 posts

    Posted 22 Sep Link to this post

    Hi Christine,

    I think the issue lies in the line 35, where the opposite collection 
    .WithOpposite(x => x.WrittenOrders)
    is declared. Can you for testing remove this mapping? In case it does not help I will take a deeper look into this tomorrow.

    Regards,
    Thomas
    Telerik by Progress
     
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
  3. DevCraft banner
  4. Christine
    Christine avatar
    5 posts
    Member since:
    Jul 2016

    Posted 29 Sep Link to this post

    It probably was something with the mapping. Unfortunately, my manager had me pull this and go back to using Entity Framework. Online documentation on the mapping could be improved for others who want to use this.
  5. Doroteya
    Admin
    Doroteya avatar
    502 posts

    Posted 05 Oct Link to this post

    Hi Christine,

    Thank you for getting back to us.

    By design, Data Access offers the so called persistence by reachability feature, which means that if you have a graph of new objects when you do an insert in the database, adding to the context only the top-most object in the graph is sufficient for the whole graph to be inserted. More details about the feature are available in this section in our documentation.

    Regarding the specific case on your side, I would kindly ask you for the following:
    1. The code of the model (the context class, the metadata source class, and the two persistent classes).
    2. DDL script of database.
    3. The text message of any errors you are experiencing.

    As for the documentation, let me suggest:
    - The Getting Started with Telerik Data Access document;
    - The Getting Started section in our documentation;
    - The Quick Start Scenarios section in our documentation.

    I hope this helps.

    Regards,
    Doroteya
    Telerik by Progress
     
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
Back to Top