# Working with Entities in Code

## Content



**DataSource for Entity Framework** is non-intrusive in the sense that you can do whatever you want with entities in code using the regular Entity Framework (or RIA) methods and properties and that will work well with [C1DataSource](/componentone/api/win/online-datasource/dotnet-framework-api/C1.Win.Data.Entity.4.8/C1.Win.Data.Entities.C1DataSource.html). You can add, delete and modify entities using the regular methods (no special [C1DataSource](/componentone/api/win/online-datasource/dotnet-framework-api/C1.Win.Data.Entity.4.8/C1.Win.Data.Entities.C1DataSource.html) object model is needed for that), and [C1DataSource](/componentone/api/win/online-datasource/dotnet-framework-api/C1.Win.Data.Entity.4.8/C1.Win.Data.Entities.C1DataSource.html) collections will automatically reflect changes that you make to the entities. For example, to add a new entity, you don't need to use some special [C1DataSource](/componentone/api/win/online-datasource/dotnet-framework-api/C1.Win.Data.Entity.4.8/C1.Win.Data.Entities.C1DataSource.html) method; therefore, none exists. Just add a new entity as you always do in EF (or RIA) and it will automatically appear in corresponding [C1DataSource](/componentone/api/win/online-datasource/dotnet-framework-api/C1.Win.Data.Entity.4.8/C1.Win.Data.Entities.C1DataSource.html) collections. If an [C1DataSource](/componentone/api/win/online-datasource/dotnet-framework-api/C1.Win.Data.Entity.4.8/C1.Win.Data.Entities.C1DataSource.html) collection has a _Where_ condition, it will appear there only if it satisfies the condition. Same with deleting and modifying entities: do it the regular EF (or RIA) way, and [C1DataSource](/componentone/api/win/online-datasource/dotnet-framework-api/C1.Win.Data.Entity.4.8/C1.Win.Data.Entities.C1DataSource.html) reflects the changes automatically, so they are automatically reflected in bound controls.

However, there is one important restriction that must be strictly observed. It does not limit what you can do; it only requires you in some (relatively rare) cases to add a method call to your code notifying [C1DataSource](/componentone/api/win/online-datasource/dotnet-framework-api/C1.Win.Data.Entity.4.8/C1.Win.Data.Entities.C1DataSource.html) of what you are doing:

![](https://cdn.mescius.io/document-site-files/images/94bcede7-c342-4bf9-97a8-95fb8706d41c/working_with_entities_in_code_files/image001.png) **CAUTION**

**Never fetch entities from the server by directly querying the context without notifying DataSource**.

All entities must be either fetched by one of the [C1DataSource](/componentone/api/win/online-datasource/dotnet-framework-api/C1.Win.Data.Entity.4.8/C1.Win.Data.Entities.C1DataSource.html)'s **GetItems** methods, or, if you want to fetch them by querying an object context directly, you must call the **ClientScope.AddRef** method to notify [C1DataSource](/componentone/api/win/online-datasource/dotnet-framework-api/C1.Win.Data.Entity.4.8/C1.Win.Data.Entities.C1DataSource.html) that you fetched entities from the server.

If you use the [C1DataSource](/componentone/api/win/online-datasource/dotnet-framework-api/C1.Win.Data.Entity.4.8/C1.Win.Data.Entities.C1DataSource.html) control or **ClientViewSource**, you fetch entities either implicitly if **AutoLoad** = true, or explicitly when you call the **ClientViewSource.Load** method. In both cases it is done through [C1DataSource](/componentone/api/win/online-datasource/dotnet-framework-api/C1.Win.Data.Entity.4.8/C1.Win.Data.Entities.C1DataSource.html), so [C1DataSource](/componentone/api/win/online-datasource/dotnet-framework-api/C1.Win.Data.Entity.4.8/C1.Win.Data.Entities.C1DataSource.html) is aware of the newly fetched entities. When you need to fetch entities in code without using ClientViewSource, the standard way to do it is by using one of the [C1DataSource](/componentone/api/win/online-datasource/dotnet-framework-api/C1.Win.Data.Entity.4.8/C1.Win.Data.Entities.C1DataSource.html)'s **GetItems** methods (EntityClientScope.GetItems|keyword=EntityClientScope.GetItems method, RiaClientScope.GetItems|keyword=RiaClientScope.GetItems method). Here too, [C1DataSource](/componentone/api/win/online-datasource/dotnet-framework-api/C1.Win.Data.Entity.4.8/C1.Win.Data.Entities.C1DataSource.html) is aware of the fetched entities. However, occasionally you may need to retrieve entities by issuing a query directly to the server without involving [C1DataSource](/componentone/api/win/online-datasource/dotnet-framework-api/C1.Win.Data.Entity.4.8/C1.Win.Data.Entities.C1DataSource.html). For example, in Entity Framework you can take an ObjectContext that is used by [C1DataSource](/componentone/api/win/online-datasource/dotnet-framework-api/C1.Win.Data.Entity.4.8/C1.Win.Data.Entities.C1DataSource.html) and create queries:

```auto
ObjectContext context = scope.ClientCache.ObjectContext;
```

// or

```auto
ObjectQuery query = ((NORTHWNDEntities)context).Customers;
```

// or

```auto
query = context.CreateObjectSet<Customer>();
```

// or

```auto
query = context.CreateQuery<Customer>("...");
```

In RIA Services such code can look like this:

```auto
DomainContext context = scope.ClientCache.DomainContext;
```

```auto
var query = ((DomainService1)context).Customers;
```

// or

```auto
var entities = context.Load(
```

```auto
((DomainService1)context).GetCustomersQuery()).Entities;
```

Having a query, you can get entities directly from the server by enumerating the query result

foreach (Customer c in query) /\* do something \*/

or by binding a control to it:

```auto
dataGrid.ItemsSource = query;
```

If you do this without calling **ClientScope.AddRef**, you can get entities from the server without [C1DataSource](/componentone/api/win/online-datasource/dotnet-framework-api/C1.Win.Data.Entity.4.8/C1.Win.Data.Entities.C1DataSource.html) knowing about it. Those entities will be in the same cache with other entities, indistinguishable from them, so [C1DataSource](/componentone/api/win/online-datasource/dotnet-framework-api/C1.Win.Data.Entity.4.8/C1.Win.Data.Entities.C1DataSource.html) will manage them as other entities, including possibly releasing them, evicting them from the cache when it does not need them. That will make the entities inaccessible, but your program may still need them! So, very bad things can happen if you fetch entities to a [C1DataSource](/componentone/api/win/online-datasource/dotnet-framework-api/C1.Win.Data.Entity.4.8/C1.Win.Data.Entities.C1DataSource.html)-governed context without using [C1DataSource](/componentone/api/win/online-datasource/dotnet-framework-api/C1.Win.Data.Entity.4.8/C1.Win.Data.Entities.C1DataSource.html) and without notifying it that entities are fetched. Fortunately, adding that notification is easy, just call **ClientScope.AddRef** for all fetched entities like this:

```auto
foreach (Customer c in query)
```

```auto
{
```

```auto
scope.AddRef(c);
```

```auto
// do something
```

```auto
}
```

It will tell [C1DataSource](/componentone/api/win/online-datasource/dotnet-framework-api/C1.Win.Data.Entity.4.8/C1.Win.Data.Entities.C1DataSource.html) that the entities should not be released as long as the scope is alive.