3

I am trying to store a Json serialized EF object in another database.

One of the items I'm strying to store is the cart, which has some relateded tables.

How can I store the cart, without dragging its relations along (preferably without resorting to .Select() to handpick the distinct columns)

[AllowAnonymous]
public ActionResult Test() {

    using (var db = new DALEntities())
    {
        var q = db.tblCarts.SingleOrDefault(x => x.CartItemID == 4275);

        q.tblContactsExtra = null;
        q.TBLINVENTORY = null;

        var settings = new JsonSerializerSettings {PreserveReferencesHandling = PreserveReferencesHandling .Objects} ;

        var str = JsonConvert.SerializeObject(q, settings);

        return Content(str);
    }
6
  • So you want to break your own referential integrity? Just set the foreign keys to allow nulls. Commented Apr 24, 2013 at 14:19
  • I only want to store, for this small purpose, the single table...if I add in all the referential data, the amount stored increases by several orders of magnitude. I don't need to reference that data in this case, so I also wish not to store it. Commented Apr 24, 2013 at 14:23
  • You'd have to set the foreign keys to allow nulls Commented Apr 24, 2013 at 14:24
  • and then the code above will operate? or will I have to do something else too? Commented Apr 24, 2013 at 14:25
  • @reidLinen That's it, the code should then work fine. Commented Apr 24, 2013 at 14:27

1 Answer 1

2

Unfortunately, you'll have to set your foreign keys to allow nulls. Otherwise you won't be able to achieve what you're looking to achieve. To do that, just make the ID fields nullable, something like:

public int? ContactsExtraId { get; set; }
public virtual tblContactsExtra tblContactsExtra { get; set; }

public int? InventoryId { get; set; }
public virtual TblInventory TBLINVENTORY { get; set; }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.