<!--
{
  "availability" : [
    "iOS: 17.0.0 -",
    "iPadOS: 17.0.0 -",
    "macOS: 14.0.0 -",
    "tvOS: 17.0.0 -",
    "watchOS: 10.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/View/modelContainer(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI",
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4ViewP01_a5Data_aB0E14modelContaineryQr0aD005ModelF0CF"
  },
  "title" : "modelContainer(_:)"
}
-->

# modelContainer(_:)

Sets the model container and associated model context in this
view’s environment.

```
@MainActor @preconcurrency func modelContainer(_ container: ModelContainer) -> some View
```

## Parameters

`container`

The model container to use for this view.

## Discussion

In this example, `ContentView` sets a model container to use for
`RecipesList`:

```
struct ContentView: View {
    @State private var container = ModelContainer(...)

    var body: some Scene {
        RecipesList()
            .modelContainer(container)
    }
}
```

The environment’s [`modelContext`](/documentation/SwiftUI/EnvironmentValues/modelContext) property will be
assigned a new context associated with this container. All implicit
model context operations in this view, such as `Query`
properties, will use the environment’s context.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)